Copy Code code as follows:
Findviewbyid (R.id.mybutton). Setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Do stuff
}
});
The disadvantage of adding listener with the above method is that if there are too many controls, the number of listener will increase, so you can use the following tips to reduce the number of listener:
Copy Code code as follows:
View.onclicklistener handler = View.onclicklistener () {
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.ID.BUTTON01://Dostuff
Break
Case R.ID.BUTTON02://Dostuff
Break
}
}
}
Findviewbyid (R.id.mybutton). Setonclicklistener (handler);
Findviewbyid (R.id.myotherbutton). Setonclicklistener (handler);
In Android1.6, the work of adding listener is quite simple (feeling more like doing web programming!). ), the steps are as follows:
1. First define the button inside the layout and specify the response listener
Copy Code code as follows:
<xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
<button
android:text= "Button01"
Android:id= "@+id/button01"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:onclick= "MyClickHandler01"
/>
<button
android:text= "Button02"
Android:id= "@+id/button02"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:onclick= "MyClickHandler02"
/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
/>
</LinearLayout>
The following two lines are the new features:
android:onclick= "MyClickHandler01"
android:onclick= "MyClickHandler02"
2. Define public methods in the activity MyClickHandler01, and MYCLICKHANDLER02 (note that the two methods must have a view parameter).
Copy Code code as follows:
Package com.ray.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
public class Testonclicklistener extends activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
public void MyClickHandler01 (View target) {
Settitle ("MyClickHandler01");
}
public void MyClickHandler02 (View target) {
Settitle ("MyClickHandler02");
}
}
Of course, you can also use this type of wording:
Set two buttons to the same listener
android:onclick= "Myclickhandler"
android:onclick= "Myclickhandler"
Copy Code code as follows:
Package com.ray.test;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
public class Testonclicklistener extends activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
}
public void Myclickhandler (View target) {
Switch (Target.getid ()) {
Case R.ID.BUTTON01:
Settitle ("MyClickHandler01");
Break
Case R.ID.BUTTON02:
Settitle ("MyClickHandler02");
Break
}
}
}