In Android, we can make the UI interact with the user through event handling (UI events).
UI user event handling, which is the operation of the view processing user, is almost inevitable in the application. View is an important class, it is the front line of interaction with the user, and in the design of the Android framework, the event Listener (listener) is the way to handle user events in the UI.
The Android framework provides a very good UI event handling mechanism. View is the class that draws the UI, and each view object can register an event listener with the Android framework. Each event listener contains a callback function (callback method), and the main job of this callback function (callback method) is to respond to or manipulate the user's actions.
Event Listener:click Listener Example
As a "user touch" action, the view is registered with the Android framework when it is dealing with a user-touched event. Onclicklistener event Listener; When the "touch" event occurs, the Android framework backs up the callback function in the event listener.
View.onclicklistener is the click Listener, as the name implies, this is the "click action listener for the UI, and when the user click on the view (that is, the UI component on the touch), the Android framework will call back to the view. The callback function of the Onclicklistener.
The callback function for View.onclicklisterner is onclick ().
The listener mentioned here refers to the event listener, which is mainly used to "monitor" the user's various actions. In addition to View.onclicklistener, the Android framework has the following event listener (and its callback method):
View.OnLongClickListener:onLongClick ()
View.OnFocusChangeListener:onFocusChange ()
View.OnKeyListener:onKey ()
View.OnTouchListener:onTouch ()
View.OnCreateContextMenuListener:onCreateContextMenu ()
Another mechanism for UI events is event handler, where event handler is a different processing mechanism than event listener. This will be studied later.
Use the View.onclicklistener provided by Android to illustrate how the program is implemented. A better approach is to implement the View.onclicklistener interface in the Acitivty class, namely:
public class Yypclicklisteneractivity extends Activity implements view.onclicklistener{
...
}
The above code uses the object-oriented interface technology, each view can register an event listener, when the Android framework receives "click" event, then callback event listener callback method. In the case of the button class, when we want to handle the user Touch button event, we call the button class's Setonclicklistener () method to register the click Listener. The above approach is to implement view.onclicklistener directly in the activity class yypclicklisteneractivity, so the click Listener of the button class above is "this". Such as:
The program code for registering click Listener is as follows:
Button button = (button) Findviewbyid (R.ID.BTN);
Button.setonclicklistener (this);
The button component is found in OnCreate (), its click Listener is this, and then the OnClick () is implemented in the Activity class. The code for the OnClick () method is as follows, and we use the toast class to respond to the message to the user:
public void OnClick (View v) {
Toast.maketext (This, "http://www.linuxidc.com", Toast.length_long). Show ();
}
The complete code is simple:
Package com.android;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.Toast;
public class Yypclicklisteneractivity extends Activity implements view.onclicklistener{
/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super. OnCreate (Savedinstancestate);
Setcontentview (R.layout.main);
Button button = (button) Findviewbyid (R.ID.BTN);
Button.setonclicklistener (this);
}
public void OnClick (View v) {
Toast.maketext (This, "http://www.linuxidc.com", Toast.length_long). Show ();
}
}
Android Event listeners event Listener