Learn android from scratch (android event handling method. 24 .)

Source: Internet
Author: User

Learn android from scratch (android event handling method. 24 .)

There are a variety of events in android, each of which has its own Processing Mechanism


For example

1 Click Event View. OnClickListener Public abstract void onClick (View v) Triggered when a component is clicked
2 Click Event View. OnLongClickListener Public abstract boolean onLongClick (View v) Triggered when long-pressed Components
3 Keyboard Events View. OnKeyListener Public abstract boolean onKey (View v, int keyCode, KeyEvent event) Handle Keyboard Events
4 Focus event View. OnFocusChangeListener Public abstract void onFocusChange (View v, boolean hasFocus) Triggered when the focus changes
5 Touch event View. OnTouchListener Public abstract boolean onTouch (View v, MotionEvent event) Generate touch events
6 Create context menu View. OnCreateContextMenuListener Public abstract void onCreateContextMenu (ContextMenu menu, View v, ContextMenu. ContextMenuInfo menuInfo) Triggered when the context menu is created

Listener Method

1 Public void setOnClickListener (View. OnClickListener l) Normal Register a click event
2 Public void setOnLongClickListener (View. OnLongClickListener l) Normal Registration duration event
3 Public void setOnKeyListener (View. OnKeyListener l) Normal Register Keyboard Events
4 Public void setOnFocusChangeListener (View. OnFocusChangeListener l) Normal Registration focus change event
5 Public void setOnTouchListener (View. OnTouchListener l) Normal Register touch events
6 Public void setOnCreateContextMenuListener (View. OnCreateContextMenuListener l) Normal Register context menu events


The following uses the Onclick single-host event as an example to describe three methods of event processing in android.

1. Internal handling events

2. Anonymous internal event handling

3. Data Source Processing Event


Example:


XML file



     
      
      
      
 




JAVA files

Package com. example. actionoperator; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; public class MainActivity extends Activity {public Button button1, button2, button3; // get the response object ButtonOnClickListener click = new ButtonOnClickListener (); @ jsonvoid onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); super. setContentView (R. layout. activity_main); button1 = (Button) this. findViewById (R. id. button1); button2 = (Button) this. findViewById (R. id. button2); button3 = (Button) this. findViewById (R. id. button3); // use an internal class to process button1.setOnClickListener (new MyListener (); // use an anonymous internal class to process button2.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "listener processing for methods of anonymous internal classes", 2 ). show () ;}}); // use the data source method for processing. It can be said that all standalone events are registered on this click to process button3.setOnClickListener (click );} class ButtonOnClickListener implements OnClickListener {public void onClick (View v) {// The reader can set the switch case statement here to determine the ID of the passed parameter to implement the same method for processing multiple events if (v = button3) {Toast. makeText (MainActivity. this, "using data sources for method listening", 2 ). show () ;}} class MyListener implements OnClickListener {@ Overridepublic void onClick (View view) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, "internal class method implementation method listener processing", 2 ). show ();}}}


Final Effect



All event handling methods are basically three of the above, and we hope that readers can master them carefully.



Next forecast:

Dialog box component

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.