The first is usually defined directly in the OnCreate event of the activity component and is directly acting. This approach is defined once for each control and is usually inconvenient.
Button btn = (button) Findviewbyid (R.id.mybutton);
Btn. Setonclicklistener (New View.onclicklistener () {
public void OnClick (View v) {
Do something
}
});
The second is usually to implement its interface in the activity component, so that multiple external controls can share an interface, which is relatively convenient
public class Testmedia extends Activity implements view.onclicklistener{
Button btn1 = (button) Findviewbyid (R.id.mybutton1);
Button btn2 = (button) Findviewbyid (R.id.mybutton2);
Btn1. Setonclicklistener ();
Btn2. Setonclicklistener ();
}
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id. MyButton1:
Do something
Break
Case R.id. MyButton2:
Do something
Break
}
A third, similar to the second, has the advantage of having more than one listening interface, which is clearer
public class Testmedia extends Activity {
Button btn1 = (button) Findviewbyid (R.id.mybutton1);
Button btn2 = (button) Findviewbyid (R.id.mybutton2);
Btn1. Setonclicklistener (New Clickevent ());
Btn2. Setonclicklistener (New Clickevent ());
}
Class Clickevent implements View.onclicklistener
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id. MyButton1:
Do something
Break
Case R.id. MyButton2:
Do something
Break
}
3 Ways to monitor Wang Liping--android events