There are a number of events in Android, each of which has its own corresponding processing mechanism.
As in the following several
1 |
Click events |
View.onclicklistener |
public abstract void OnClick (View v) |
Triggered when a component is clicked |
2 |
Click events |
View.onlongclicklistener |
Public abstract Boolean Onlongclick (View v) |
Triggered when the component is long pressed |
3 |
Keyboard events |
View.onkeylistener |
Public abstract Boolean OnKey (View v, int keycode, keyevent event) |
Handling Keyboard Events |
4 |
Focus Event |
View.onfocuschangelistener |
public abstract void Onfocuschange (View V, boolean hasfocus) |
Trigger when focus changes |
5 |
Touch events |
View.ontouchlistener |
Public abstract Boolean OnTouch (View V, motionevent event) |
Generate Touch Events |
6 |
Create a context Menu |
View.oncreatecontextmenulistener |
public abstract void Oncreatecontextmenu (ContextMenu menu, View V, Contextmenu.contextmenuinfo menuinfo) |
triggered when the context menu is created |
Monitoring Methods
1 |
public void Setonclicklistener (View.onclicklistener l) |
Ordinary |
Registering Click events |
2 |
public void Setonlongclicklistener (View.onlongclicklistener l) |
Ordinary |
Registering Long Press Events |
3 |
public void Setonkeylistener (View.onkeylistener l) |
Ordinary |
Registering keyboard events |
4 |
public void Setonfocuschangelistener (View.onfocuschangelistener l) |
Ordinary |
Register Focus Change Event |
5 |
public void Setontouchlistener (View.ontouchlistener l) |
Ordinary |
Registering Touch Events |
6 |
Public void Setoncreatecontextmenulistener (view.oncreatecontextmenulistener L) |
Ordinary |
Registering context Menu Events |
The following is an example of an onclick stand-alone event that illustrates three ways to handle events in Android
1. Internal class handling events
2. Anonymous internal class handling events
3. Data Source Handling Events
Examples are as follows:
XML file
<span style= "FONT-SIZE:18PX;" ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <button android:id=" @+id/button1 "android:layout_width=" Wrap_content "Andro id:layout_height= "Wrap_content" android:layout_alignparenttop= "true" android:layout_centerhorizontal= "true" android:layout_margintop= "60DP" android:text= "Way One"/> <button android:id= "@+id/button2" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignleft= "@+id/b Utton1 "Android:layout_below= "@+id/button1" android:layout_margintop= "44DP" android:text= "way Two"/> <Button Android:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_alignleft= "@+id/button2" android:layout_below= "@+id/button2" android:layout_margintop= "35d P "android:onclick=" click "android:text=" mode three "/> </RelativeLayout></span>
Java files
<span style= "FONT-SIZE:18PX;" >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 Bubuttononclicklistener object Buttononclicklistener click =new buttononclicklistener (); @Overrideprotected void 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 internal classes for processing Button1.setonclicklistener (new MyListener ());// Working with anonymous inner classes Button2.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO Auto-generated method Stubtoast.maketext (Mainactivity.this, "methods of anonymous internal classes to implement the method of listening processing", 2). Show ();}); /Use the data source method to process, can say all single-machine thingsRegister on this click to process Button3.setonclicklistener (click);} Class Buttononclicklistener implements Onclicklistener {public void OnClick (View v) {//reader can set the switch case statement here to the ID of the passed parameter The same method that implements multiple events is used to process if (V==button3) {Toast.maketext (mainactivity.this, "Use data source implementation Method listener processing", 2). Show (); }}class MyListener implements Onclicklistener {@Overridepublic void OnClick (view view) {//TODO auto-generated method stub Toast.maketext (Mainactivity.this, "method implementation method for inner class listener processing", 2). Show ();}} </span>
Final effect
All the handling of the event is basically the above three kinds of processing mechanism, I hope the reader must carefully grasp
The next section predicts:
Dialog dialog box component