分类: 嵌入式
2011-10-18 15:34:17
// 按键按下触发事件
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
DisplayToast("按下:中键");
break;
case KeyEvent.KEYCODE_DPAD_UP:
DisplayToast("按下:向上键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
DisplayToast("按下:下方键");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
DisplayToast("按下:左方键");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
DisplayToast("按下:右方键");
break;
}
return super.onKeyDown(keyCode, event);
}
二、 弹起按键触发事件:
// 按键弹起触发事件
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
DisplayToast("弹起:中键");
break;
case KeyEvent.KEYCODE_DPAD_UP:
DisplayToast("弹起:向上键");
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
DisplayToast("弹起:下方键");
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
DisplayToast("弹起:左方键");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
DisplayToast("弹起:右方键");
break;
}
return super.onKeyUp(keyCode, event);
}
三、 笔触事件响应
// 笔触事件
public boolean onTouchEvent(MotionEvent event) {
int iAction = event.getAction();
if (iAction == MotionEvent.ACTION_CANCEL
|| iAction == MotionEvent.ACTION_DOWN
|| iAction == MotionEvent.ACTION_MOVE) {
return false;
}
int x = (int) event.getX();
int y = (int) event.getY();
DisplayToast("触笔点击坐标:(" + Integer.toString(x) + ","
+ Integer.toString(y) + ")");
return super.onTouchEvent(event);
}
四、 onKeyMultiple
指多个连续的重复事件,可拆开为多个上面的ACTION_DOWN和ACTION_UP理解,直接接收到ACTION_MULTIPLE这个KeyEvent的时候,onKeyMultiple才会触发
public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
return super.onKeyMultiple(keyCode, repeatCount, event);
}
五、 构造返回键
public boolean onKeyDown(int keyCode,KeyEvent event){
KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK);
return super.onKeyDown(key.getkeyCode(),key);
}