1:视频
http://edu.51cto.com/lesson/id-15687.html
马老师的 android视频教程第五节
2:遇到问题
The method setOnClickListener(View.OnClickListener) in the type View is not
解决方法:
http://www.cnblogs.com/yigerendehaibian/archive/2012/12/08/2809141.html
http://bbs.51cto.com/thread-1024351-1.html
3:注意第4行和第8行只能存在一个,否则会造成冲突
MainActivity.java
1 package com.helloworld1; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.support.v7.app.ActionBarActivity; 6 //import android.content.DialogInterface.OnClickListener; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.view.MenuItem; 12 import android.widget.Button; 13 import android.widget.TextView; 14 import android.content.DialogInterface; 15 16 17 public class MainActivity extends ActionBarActivity { 18 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 TextView myTextView = (TextView)findViewById(R.id.MyTextView); 24 Button myButtonView = (Button)findViewById(R.id.MyButtonView); 25 myTextView.setText("我的第一个Text View"); 26 myButtonView.setText("我的第一个Button View" + " " + "guolong"); 27 findViewById(R.id.MyButtonView).setOnClickListener(new MybuttonListener()); 28 29 } 30 31 class MybuttonListener implements OnClickListener 32 { 33 public void onClick(View v) { 34 Intent intent = new Intent(MainActivity.this, OtherActivity.class); 35 intent.putExtra("data", "mainActivity"); 36 startActivity(intent); 37 38 } 39 }
OtherActivity.java
1 package com.helloworld1; 2 3 4 import android.support.v7.app.ActionBarActivity; 5 import android.content.Intent; 6 import android.os.Bundle; 7 import android.view.Menu; 8 import android.view.MenuItem; 9 import android.widget.Button; 10 import android.widget.TextView; 11 12 public class OtherActivity extends ActionBarActivity{ 13 private TextView myTextView = null; 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 // TODO Auto-generated method stub 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.other_main); 19 Intent intent = getIntent(); 20 String value = intent.getStringExtra("data"); 21 myTextView = (TextView)findViewById(R.id.MyTextView); 22 myTextView.setText(value); 23 } 24 }