Implementation of click events in Android, android events
In the previous blog posts, I used the click event processing implementation multiple times. Some friends asked me how to click many buttons. However, many blog posts use different implementation methods, what is going on. Today, we will summarize the implementation of click events.
Click events are implemented in the following three ways:
(1) Click events implemented by Activity through interfaces (frequently used)
(2) Use the configuration file android: onclick to customize the method.
(3) Implement using internal classes
(4) Implement using anonymous internal classesThe following describes how to implement click events:
The following code demonstrates how to implement click events: (1)
Activity implementation interface implementation Click Event
1/** 2 * The Activity implements the interface mode and click the event. 3 * The Activity implements the View. onClickListener implements onClick (View view) {} Method 4 * registers event 5 */6 public class MainActivity extends AppCompatActivity implements View in onCreate method of Activity. onClickListener {7 private Button btn; 8 protected void onCreate (Bundle savedInstanceState) {9 super. onCreate (savedInstanceState); 10 setContentView (R. layout. activity_main); 11 btn = (Button) findViewById (R. id. button); 12 btn. setOnClickListener (this); 13} 14 public void onClick (View v) {15 Toast. makeText (MainActivity. this, "interface implementation method", Toast. LENGTH_LONG ). show (); 16} 17}
(2) Use the layout configuration file android: onclick
1/** 2 * use the configuration file to implement click events. 3 * use The onClick attribute in the configuration file of layout to specify the processing method for triggering events, 4 * provide a method with the same name in the Activity in the format of public void XXX (View v ){....} 5 */6 public class MainActivity extends AppCompatActivity {7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_main); 10} 11 public void click (View v) {12 Toast. makeText (MainActivity. this, "Custom", Toast. LENGTH_LONG ). show (); 13} 14}
(3) Implement using internal classes
1/** 2 * use internal class to implement click events 3 * define an implementation class of View. OnClickListener to implement The onClick method. 4 * register the event 5 */6 public class MainActivity extends AppCompatActivity {7 private Button btn; 8 protected void onCreate (Bundle savedInstanceState) {9 super in the onCreate method of the Activity. onCreate (savedInstanceState); 10 setContentView (R. layout. activity_main); 11 btn = (Button) findViewById (R. id. button2); 12 btn. setOnClickListener (new Listener (); 13} 14 class Listener implements View. onClickListener {15 @ Override16 public void onClick (View v) {17 Toast. makeText (MainActivity. this, "internal class", Toast. LENGTH_LONG ). show (); 18} 19} 20}
(4)
Implement using anonymous internal classes
1/** 2 * click an event in the form of an anonymous internal class. 3 * when the event registration Button is clicked, the anonymous internal class is directly used. 4 */5 public class MainActivity extends AppCompatActivity {6 private Button btn; 7 protected void onCreate (Bundle savedInstanceState) {8 super. onCreate (savedInstanceState); 9 setContentView (R. layout. activity_main); 10 btn = (Button) findViewById (R. id. button3); 11 btn. setOnClickListener (new View. onClickListener () {12 @ Override13 public void onClick (View v) {14 Toast. makeText (MainActivity. this, "anonymous internal class", Toast. LENGTH_LONG ). show (); 15} 16}); 17} 18}
Supplement:
Different components are used to implement different types of click events, such as onItemClickListener, listener, listener, OnDate/timeSetListener, OnScrollListener, OnChildClickListener, setOnTouchListener, OnPageChangeListener, listener, oneditexceptionlistener, oneditexceptionlistener and so on.