/**
* Four Ways to type button
*/
The first gets the button and then adds a listen event to the button, creating a class that implements the Onclicklistener interface.
The response event of a button is implemented in the interface class.
Button callbtn = (button) This.findviewbyid (R.ID.CALLBTN);
Callbtn.setonclicklistener (New MyListener ());
To create a class that implements an interface
public class MyListener implements Onclicklistener {
@Override
public void OnClick (View v) {
/**
* Method implementation
*/
}
}
The second is to create an instance of the interface directly when the listener is added
Button callbtn = (button) This.findviewbyid (R.ID.CALLBTN);
Callbtn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
/**
* Specific implementation
*/
}
});
The third is to let mainactivity Implement Interface Onclicklistener
public class Mainactivity extends Activity implements Onclicklistener {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button callbtn = (button) This.findviewbyid (R.ID.CALLBTN);
Callbtn.setonclicklistener (this);
}
@Override
public void OnClick (View v) {
/**
* When there are many buttons, let the current mainactivity inherit the Onclicklistener interface,
In the method to be implemented, the switch case statement is used to determine
*
*/
Switch (V.getid ()) {
Case R.ID.CALLBTN:
Break
Default
Break
}
}
}
}
Fourth add android:onclick= "Callphone" in Activity_main.xml <button/>
and make it in Mainactivity.java.
is actually achieved through reflection.
public class Mainactivity extends Activity {
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}
public void callbtnclicked (View v) {
/**
* Specific implementation
*/
}
}
Four Ways to implement button