Learn how to handle android events from scratch <android events. 24.> and android events from scratch
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
<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: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" tools: context = ". mainActivity "> <Button android: id =" @ + id/button1 "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignParentTop =" true "android: layout_centerHorizontal = "true" android: layout_marginTop = "60dp" android: text = "method 1"/> <Button android: id = "@ + id/button2" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignLeft = "@ + id/button1" android: layout_below = "@ + id/button1" android: layout_marginTop = "44dp" android: text = "method 2"/> <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 = "35dp" android: onClick = "click" android: text = "method 3"/> </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 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 () ;}}</span>
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
How can I learn android development from scratch (JAVA knows little about it)? How long can I start programming on my own?
If you want to write a basic android book while reading it, You can also read it. (In my opinion, the java basics must be good for android. If you want to learn it now, you may feel that everything in the book will work, but you will find a lot of problems. no. Compared to android, you must learn java basics first. android can learn real skills in the field. If it is better, you can make better progress in the future. Otherwise, you will not have to review the lessons of java while working like me) good method: If I don't have to write more and get more information, there will basically be a lot of online information. I generally go to forums such as eoe to ask for the basics. Most of the books are very easy to understand at the entry level (you know something about java)
How did cainiao learn android to allow android to respond to the press and pop-up events separately? Kneel
Onclicklistener is a click listener. You must use ontouchlistener to listen to the push and lift events respectively.
This is the code:
B1.setOnTouchListener (new OnTouchListener (){
@ Override
Public boolean onTouch (View v, MotionEvent event ){
Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
// Press
Break;
Case MotionEvent. ACTION_MOVE:
// Move
Break;
Case MotionEvent. ACTION_UP:
// Lift
Break;
}
Return true;
}
});