Steps:
1.tell android you are interested in listening to a button click
2.bring your xml button inside java
3.tell your java button whose responding when it‘s clicked
4.what should happen when button is clicked
1 <Button 2 android:layout_width="wrap_content" 3 android:layout_height="wrap_content" 4 android:text="Button1" 5 android:id="@+id/button1" 6 android:layout_below="@+id/textView" 7 android:layout_alignParentLeft="true" 8 android:layout_alignParentStart="true" />
1 public class MainActivity extends Activity implements OnClickListener {//1.tell android you are interested in listening to a button click 2 3 Button button; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 button = (Button)findViewById(R.id.button1);//2.bring your xml button inside java 9 button.setOnClickListener(this);//3.tell your java button whose responding when it‘s clicked 10 } 11 12 @Override//4.what should happen when button is clicked 13 public void onClick(View v) { 14 Log.e("MainActivity","Clicked1"); 15 } 16 17 }
原文:http://www.cnblogs.com/turtle920/p/4860740.html