click-to-respond events in Android are mostly response to events with a button, about four ways to respond to Events1,, create an inner class that defines the click event2, create an anonymous inner class that implements the interface directly3,interface for activity implementation of click events (most common cases used)4,4, a click method is bound in the layout file <Button Android:onclick= "dailbuttonclicked"...../>Some of the code for this process is based on the implementation of a simple phone Dialer
public class Mainactivity extends Activity implements Onclicklistener {private EditText ed_number; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Load Layout file Setcontentview (r.layout. Activity_main); Find button bt_dail= (button) this. Findviewbyid (R.id.button1); Find text Box ed_number= (EditText) MainActivity.this.findViewById (R.id.et_number); Register Click event//1 for the button, create an inner class, define the Click event/* Bt_dail.setonclicklistener (New MyListener ()); *///2, the way to create an anonymous inner class /* Bt_dail.setonclicklistener (New Onclicklistener () {@Override Pub LIC void OnClick (View v) {callphone (); }}); *//* 3, let activity implement the interface of click events (most common cases used) * Bt_dail.setonclicklistener (this); */} 4, in the layout file is bound a click Method public voID dailbuttonclicked (view view) {Callphone (); }The specific method implementation is shown in the following code:
private void Callphone () { String number= ed_number.gettext (). toString (). Trim (); if (textutils. IsEmpty (number)) { toast. Maketext (mainactivity.this, "numbers cannot be empty", toast. Length_short). Show (); return; } Intent intent= new Intent (); Intent.setaction (Intent. Action_call); URI type intent.setdata (URI. Parse ("Tel:" +number)); StartActivity (intent); } Private class MyListener implements onclicklistener{ / * * button is called when clicked */ @Override public void OnClick (View v) { callphone (); } } @Override public void OnClick (View v) { //TODO auto-generated method Stub callphone (); }
Four ways to read about click Response Events in Android (Phone Dialer)