常见的事件处理:
boolean onKeyDown(int keyCode, KeyEvent event)用于相应按键按下;
boolean onKeyMultiple(int keyCode, KeyEvent event)用于响应按键重复点击;
boolean onKeyUp(int keyCode, KeyEvent event)用于响应按键释放
boolean onTouchEvent(MotionEvent event)用于响应触摸屏事件
直接上代码:
package com.yarin.android.Examples_04_01;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button_ok = (Button) findViewById(R.id.ok);
button_ok.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
DisplayToast("点击了ok按钮");
}
});
}
/* 按键按下 */
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 onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
{
return super.onKeyMultiple(keyCode, repeatCount, 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);
}
/* 显示Toast */
public void DisplayToast(String str)
{
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
每个键都对应一个键值。可以根据需要来改变一些键的功能,这就需要自己构建KeyEvent对象,构造KeyEvent对象的
方式:
KeyEvent(int action, int code);
KeyEvent(long DownTime, long EventTime, int action, int code, int repeat);
......
package com.yarin.android.Examples_04_02;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//构建KeyEvent对象
KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
return super.onKeyDown(key.getKeyCode(), key);
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yujixi123/archive/2010/09/16/5889145.aspx