Handling input events in Android
Selasa, 10 Juli 2012
0
komentar
Handling Input Events in Android
The input events are the key elements for the user interface on mobile devices. The mobile devices has many ways to interact with device need to be handle in programming.
In android you can intercept the events passed by the user during interaction with your app and implement the callback to process the events. In android the activity presents the view to the user and each view object will generate the events based on user interaction.
This events could be intercept by each view object using the callback methods or event listeners. In case if you are creating your custom view object you can implement the event handlers for your view object.
Event Listeners
An event listener is an interface in the View class that contains a single callback method. These methods will be called by Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the user interface.
See the list of event listener callback methods here
Event | Listener Callback method | Description |
onClick() | View.onClickListener | This is called when the user either touches the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses the suitable "enter" key or presses down on the trackball. |
onLongClick() | View.onLongClickListener | This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second). |
onFocusChange() | View.onFocusChangeListener | This is called when the user navigates onto or away from the item, using the navigation-keys or trackball. |
onKey() | onKeyListener | This is called when the user is focused on the item and presses or releases a hardware key on the device. |
onTouch() | onTouchListener | This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item). |
onContextMenu() | onContextMenuListener | This is called when a Context Menu is being built (as the result of a sustained "long click"). |
You can use the View.setEventListener() to register the events with view object.
There are two ways to implement the event listener for a view object.
Register the event listener with view object
Example Code :
// Create an raw implementation of OnClickListener
private OnClickListener mTestListener = 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.testbutton);
// Register the onClick listener with the implementation above
button.setOnClickListener(mTestListener);
...
}
Implement the event listener on activity class
Example Code :
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
}
...
}
onLongClick(),onKey() and onTouch() events return a boolean value. If return true then the event will not propagate further to event listener.
Event Handlers
An event handler is required to implement when you are extending the view object to create your own view object.
You can implement the following basic event handler
- onKeyDown(int, KeyEvent) - Called when a new key event occurs.
- onKeyUp(int, KeyEvent) - Called when a key up event occurs.
- onTrackballEvent(MotionEvent) - Called when a trackball motion event occurs.
- onTouchEvent(MotionEvent) - Called when a touch screen motion event occurs.
- onFocusChanged(boolean, int, Rect) - Called when the view gains or loses focus.
Did you like the post? so this is the time to share.
Please provide your valuable comments to improve our post.
Creative Android Apps
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Handling input events in Android
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://apk-xda.blogspot.com/2012/07/handling-input-events-in-android.html. Terima kasih sudah singgah membaca artikel ini.Ditulis oleh Unknown
Rating Blog 5 dari 5
0 komentar:
Posting Komentar