I used the click event processing implementation many times in my previous blog posts. Some friends asked and found that many button click implementations were implemented, but many blog posts used different implementation methods. What exactly is going on. Today we will summarize the implementation of click events.
The implementation of click events is roughly divided into the following three types:
(1) Activity implements interface method to implement click events (used frequently)
(2) Customize the method, use the configuration file android:onclick
(3) Implementation using internal class
(4) Use anonymous internal class implementation to introduce the following ways to implement click events:
Below we will briefly demonstrate the following ways to implement click events through code:
(1) Activity implements the interface method to implement click events
/** * Activity implements the interface method to implement click events * Activity implementation Implement onClick(View view){} method * Register events in the onCreate method of the Activity */ public class MainActivity extends AppCompatActivity implements { private Button btn; protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); btn = (Button) findViewById(); (this); } public void onClick(View v) { (,"Implementing interface method",Toast.LENGTH_LONG).show(); } }
(2) Customize the method, use layout configuration file android:onclick
/** * Use configuration file to implement click events * Use the onClick attribute in the configuration file in layout to specify the handling method when the event is triggered. * Provide a method with the same name in the Activity format is public void XXX(View v){....} */ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); } public void click(View v){ (,"Customize",Toast.LENGTH_LONG).show(); } }
(3) Implementation using internal class
/** * Use internal class to implement click events * Define an implementation class to implement the onClick method. * Register events in the onCreate method of the Activity */ public class MainActivity extends AppCompatActivity { private Button btn; protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); btn = (Button) findViewById(.button2); (new Listener()); } class Listener implements { @Override public void onClick(View v) { (,"Internal Class",Toast.LENGTH_LONG).show(); } } }
(4) Implementation using anonymous internal classes
/** * Use anonymous internal class to implement click events * Use anonymous internal classes directly when registering button click event */ public class MainActivity extends AppCompatActivity { private Button btn; protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); btn = (Button) findViewById(.button3); (new (){ @Override public void onClick(View v) { (,"Anonymous internal class",Toast.LENGTH_LONG).show(); } }); } }
Replenish:
Different components to implement different types of click events, such as onItemClickListener, OnCheckedChangeListener, OnRatingBarChangeListener, OnMultiChoiceClickListener, OnDate/timeSetListener, OnScrollListener, OnChildClickListener, setOnTouchListener, OnPageChangeListener, OnMenuItemClickListener, OnEditorActionListener, OnEditorActionListener and other various click events processing forms.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.