Key event Handling

Source: Internet
Author: User

/**
* Control events listen and handle event keystrokes by setting their space listener: press the event by overriding the OnKeyDown method: By overriding the OnKeyUp method
* Stylus Click event: By implementing the Ontouchevent method
*
* @author Chen_xhua
*
*/
public class Keyactivity extends Activity {
private static final String TAG = "keyactivity";
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_key);
Get Button Object
Button BUTTON_OK = (button) Findviewbyid (R.ID.BUTTON_OK);
Setting the button control listener
Button_ok.setonclicklistener (New Button.onclicklistener () {
public void OnClick (View arg0) {
Handling Events Here
Displaytoast ("Click OK button");
}
});
}

//Press the trigger event
Public boolean OnKeyDown (Int. KeyCode, KeyEvent event) {
Switch (keycode) {
Case Keyevent.keycode_dpad_center:
Displaytoast ("Press: Middle Key");
Break
Case KEYEVENT.KEYCODE_DPAD_UP:
Displaytoast ("Press: Up ARROW key");
Break
Case Keyevent.keycode_dpad_down:
Displaytoast ("Press: DOWN ARROW key");
Break
Case Keyevent.keycode_dpad_left:
Displaytoast ("Press: Left ARROW key");
Break
Case Keyevent.keycode_dpad_right:
Displaytoast ("Press: Right ARROW key");
Break
}
Return Super.onkeydown (KeyCode, event);
}

Press the key to lift the event that is triggered:
public boolean onKeyUp (int keycode, keyevent event) {
Switch (keycode) {
Case Keyevent.keycode_dpad_center:
Displaytoast ("Pop Up: Middle button");
Break
Case Keyevent.keycode_dpad_down:
Displaytoast ("Pop Up: down ARROW key");
Break
Case Keyevent.keycode_dpad_left:
Displaytoast ("Bounce: Left ARROW key");
Break
Case Keyevent.keycode_dpad_right:
Displaytoast ("Bounce: Right ARROW key");
Break
Case KEYEVENT.KEYCODE_DPAD_UP:
Displaytoast ("Bounce up: Up ARROW key");
Break
}
Return Super.onkeyup (KeyCode, event);
}

Press the trigger event multiple times
public boolean onkeymutiple (int keycode, int repeatcount, keyevent event) {
Return Super.onkeymultiple (KeyCode, RepeatCount, event);
}

Stylus Events
public boolean ontouchevent (Motionevent event) {
int iaction = Event.getaction ();
if (iaction = = Motionevent.action_cancel
|| IAction = = Motionevent.action_down
|| IAction = = Motionevent.action_move) {
return false;
}
Where to get a stylus click
int x = (int) event.getx ();
int y = (int) event.gety ();
Displaytoast ("Stylus Click Coordinates: (" + integer.tostring (x) + ","
+ integer.tostring (y) + ")");
Return Super.ontouchevent (event);

}

Show Toast
public void Displaytoast (String str) {
Toast.maketext (this, str, toast.length_short). Show ();
}

}

Key event Handling

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.