Android user interface-Event Processing

Source: Internet
Author: User
Tags xml attribute

Handle UI events
There are multiple ways to get users and applications on Android Program When considering the events in the UI, we can capture the events generated by a specific view object that interacts with the user.
In the view object you used to make up the layout, you may notice some callback functions used to process UI events. these methods are called by the android framework. for example, when a view is pressed, its ontouchevent () method is called. however, to intercept this information, you must extend this class and rewrite this method. extending each view object to process such events may be impractical. this is why the View class also contains a set of nested interfaces that you can easily define. these interfaces are called listeners, which are a powerful tool for capturing user actions.
Although you may be more often using event listeners to listen for user actions, sometimes you may want to do this by extending a View class method. you may want to extend the button class to do some clever things. in this case, you can use a time processor to define the default event behavior of the class.
Event Listeners event listener
An event listener is an interface of the View class. The methods contained in this interface will be called by Android when the event listener registered with the view is triggered.
There are the following methods in the event listener:
* Onclick () is located in view. onclicklistener. It is called when you touch this object, or use a trackball to focus the object, and press the "enter" key or press the trackball.
* Onlongclick () is located in view. onlongclicklistener. It is called when you press this element or the trackball.
* Onfocuschange () is located in view. onfocuschangelistener. It is called when the object gets or loses focus.
* The onkey () is located in view. onkeylistener. It is called when the object obtains the focus and presses the next key.
* Ontouch () view. ontouchlistener. When a user performs a touch action within the range of the view object, such as pressing, releasing, or moving any gesture.
* Oncreatecontextmenu () view. oncreatecontextmenulistener. When a shortcut menu is displayed (long-pressed by the user ).
These methods are only the unique methods of their corresponding interfaces. to define these methods, you can implement this interface in your activity or use an anonymous class. then, the interface instance is passed to the corresponding view. set... listener method.
Take onclicklistener as an example:
// Create an anonymous Implementation of onclicklistener
Private onclicklistener mcorkylistener = new onclicklistener (){
Public void onclick (view v ){

// Do something when the button is clicked
}
};
Protected void oncreate (bundle savedvalues ){
...
// Capture our button from Layout
Button button = (button) findviewbyid (R. Id. Corky );
// Register The onclick listener with the Implementation above
Button. setonclicklistener (mcorkylistener );
...
}
You may think it is more convenient to implement onclicklistener as part of the activity, which can avoid additional classes. For example:
Public class exampleactivity extends activity implements onclicklistener {
Protected void oncreate (bundle savedvalues ){
...
Button button = (button) findviewbyid (R. Id. Corky );
Button. setonclicklistener (this );
}
// Implement the onclicklistener callback
Public void onclick (view v ){
// Do something when the button is clicked
}
...
}
Note that onclick () does not return values, but some event listeners must have a Boolean return value. The following are some reasons:
* Onlongclick ()-returns a Boolean value indicating whether you have consumed the event. that is, if you have already processed the event, it should be stopped and true will be returned. If you have not processed it, it will be left to other on-click listeners, returns false.
* Onkey ()-same as above.
* Ontouch ()-returns a Boolean value indicating whether you have consumed the event. this event can have multiple actions. if you return false when a downward action is received, it indicates that you have not consumed the event and are not interested in subsequent actions. that is to say, the subsequent gestures and the final upward movements will not be notified.
Key events are always sent to the view with the current focus. they are distributed from the top of the view level, and then down until the appropriate destination. if your view has focus, you can view the event allocation process in the dispatchkeyevent () method. in addition to Veiw, you can also use the onkeydown () and onkeyup () Methods of your activity to get all the time.
Note: Android will first call the event processor and then call the appropriate default processor. therefore, returning true from these Event Listeners will invalidate other listeners and default processors. therefore, be careful when you return true.
Event Handlers event Processor
If you create a custom component from the view, you can define some default event processors. In the building M components document, you will see these callback functions:
* Onkeydown (INT, keyevent)-when a new keyboard time starts to be called.
* Onkeyup (INT, keyevent)-called when a key is released.
* Ontrackballevent (motionevent)-called when the trackball moves.
* Ontouchevent (motionevent)-called when the screen moves.
* Onfocuschanged (Boolean, Int, rect)-called when a view loses focus.
There are some methods that do not belong to the view, but can also directly affect the event processing:
* Activity. dispatchtouchevent (motionevent)-all events can be intercepted by the activity before these events are distributed to the window.
* Viewgroup. onintercepttouchevent (motionevent)-Let the viewgroup see these events before the event is assigned to the subview.
* Viewparent. requestdisallowintercepttouchevent (Boolean)-Prevent the parent view from using onintercepttouchevent (motionevent) to intercept the event.

touch mode
when a user uses a direction key or trackball to move the UI, the UI elements that can be moved must be focused, in this way, users can see what will get their input. If the device has the touch capability and the user uses the touch method to interact, there is no need to give an element focus. Therefore, there is an interactive mode called "touch mode ".
for a touch-able device, once the user touches the screen, the device enters the touch mode. After that, only the isfocusableintouchmode () is the real view, and the focus can be obtained, such as the text box. Other views can be touched, such as buttons, but do not get the focus when you touch them. They only start the corresponding on-click listener. When the user presses the direction key or rotates the trackball, the device will exit the touch mode and find a view and give him focus. Now, you can interact without touching the screen.
the touch mode status is maintained throughout the system. You can use isintouchmode () to query the current status.
handling focus processing focus
the android Framework processes the Focus movement based on user input. This includes changing the focus when the view is removed, hidden, or re-appeared. View uses the isfocusable () and setfocusable () Methods to indicate and set whether they can obtain the focus. In touch mode, you can use isfocusableintouchmode () and setfocusableintouchmode ()..
Focus movement is based on the algorithm of the nearest element in a certain direction. In rare cases, the default algorithm may be different from the developer's idea. In this case, you can provide an algorithm to modify the following:


XML attributes: nextfocusdown, nextfocusleft, nextfocusright, and nextfocusup. For example:
Linearlayout
Android: Orientation = "vertical"
...
Button Android:
Android: nextfocusup = "@ + ID/bottom"
.../
Button Android:
Android: nextfocusdown = "@ + ID/top"
.../
/Linearlayout
In general, in this vertical and downward layout, the first button will not go up. Join the aboveCodeThe second button gets the focus from the first button up.
If you want to set a view as the retrieved focus, add the XML Attribute Android: focusable = "true" and Android: focusableintouchmode = "true ".
When you want a view to obtain the focus, call requestfocus ().
To listen to focus events, use onfocuschange ().

 

Instance:

Code

  Package Com. Amaker. test;

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. keyevent;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. View. onfocuschangelistener;
Import Android. View. View. onkeylistener;
Import Android. widget. Button;
Import Android. widget. checkbox;
Import Android. widget. compoundbutton;
Import Android. widget. edittext;
Import Android. widget. Toast;
Import Android. widget. compoundbutton. oncheckedchangelistener;

/**
*
* Test Event
*/
Public Class Mainactivity Extends Activity {
/** Called when the activity is first created. */

Private Edittext myedit1, myedit2;
Private Checkbox CB1;
Private Button B1, B2;

@ Override
Public Void Oncreate (bundle savedinstancestate ){
Super . Oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Myedit1 = (Edittext) findviewbyid (R. Id. edittext01 );
Myedit2 = (Edittext) findviewbyid (R. Id. edittext02 );

CB1 = (Checkbox) findviewbyid (R. Id. checkbox01 );
B1 = (Button) findviewbyid (R. Id. button01 );
B2 = (Button) findviewbyid (R. Id. button02 );

// Edit button events in the text box
Myedit1.setonkeylistener ( New Onkeylistener (){
Public Boolean Onkey (view V, Int Keycode, keyevent event ){
Myedit1.settext ( "" );
Return False ;
}
});

// Edit button events in the text box
Myedit2.setonkeylistener ( New Onkeylistener (){
Public Boolean Onkey (view V, Int Keycode, keyevent event ){
Myedit2.settext ( "" );
Return False ;
}
});

// Edit the focus event of the text box
Myedit1.setonfocuschangelistener ( New Onfocuschangelistener (){
Public Void Onfocuschange (view V, Boolean Hasfocus ){
Toast. maketext (getapplicationcontext (), myedit1.gettext (),
Toast. length_long );
}
});
// Edit the focus event of the text box
Myedit2.setonfocuschangelistener ( New Onfocuschangelistener (){
Public Void Onfocuschange (view V, Boolean Hasfocus ){
Toast. maketext (getapplicationcontext (), myedit2.gettext (),
Toast. length_long );
}
});
// Select an event from the multiple selection box
Cb1.setoncheckedchangelistener ( New Oncheckedchangelistener (){
Public Void Oncheckedchanged (compoundbutton buttonview,
Boolean Ischecked ){
Toast. maketext (getapplicationcontext (), cb1.ischecked () + "" ,
Toast. length_long );
}
});
// Button selection event
B1.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Toast. maketext (getapplicationcontext (), b1.gettext (),
Toast. length_long );
}
});
// Button selection event
B2.setonclicklistener ( New Onclicklistener (){
Public Void Onclick (view v ){
Toast. maketext (getapplicationcontext (), b2.gettext (),
Toast. length_long );
}
});

}
}

 

 

Related Article

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.