|
小弟最近在学习做一个游戏,其中要用到虚拟键盘,处理键盘事件的时候,要区分onKeyUp ,onKeyLongPress,onKeyMultiple这三种,但是重写了这些方法。在实际运行过程中,只有onKeyUp 响应了,其他的均不响应
我在查官方文档的时候,是这样说的
在 onKeyDown 里If you return true, you can first call KeyEvent.startTracking() to have the framework track the event through itsonKeyUp(int, KeyEvent) and also call your onKeyLongPress(int, KeyEvent) if it occurs
意思是说如果onKeyDown 返回true,如果长按事件被触发,就会调用该方法,可是我重写了onKeyDown ,并返回true,onKeyLongPress依然无反应。
求解答
附上源码,只是小弟测试用的代码,非游戏源码
package demo.OnClick;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Demo extends Activity {
/** 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.button1);
//设置监听
button.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DisplayToast("事件触发成功");
}
});
}
//按下按键是发生
@Override
public void onUserInteraction() {
// TODO Auto-generated method stub
DisplayToast("dispatchTouchEvent");
super.onUserInteraction();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
DisplayToast("onKeyDown");
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
DisplayToast("onKeyUp");
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
DisplayToast("onKeyLongPress-------------------------------------------------");
return super.onKeyLongPress(keyCode, event);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
DisplayToast("dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean dispatchTrackballEvent(MotionEvent ev) {
// TODO Auto-generated method stub
DisplayToast("dispatchTrackballEvent");
return super.dispatchTrackballEvent(ev);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// TODO Auto-generated method stub
DisplayToast("dispatchKeyEvent");
return super.dispatchKeyEvent(event);
}
//手写笔
@Override
public boolean onTouchEvent(MotionEvent event)
{
int iAction=event.getAction();
if(iAction==MotionEvent.ACTION_MOVE)
{
DisplayToast("你在触摸屏上进行了滑动");
}
else
{
return false;
}
return true;
}
//设置多次点击时发生
@Override
public boolean onKeyMultiple(int keyCode,int repeatCount,KeyEvent event)
{
int ICount=event.getRepeatCount();
int IAction=event.getAction();
if(ICount==2&&IAction==MotionEvent.ACTION_DOWN)
{
DisplayToast("你在触摸屏上点击了两下");
}
else
{
return false;
}
return super.onKeyMultiple(keyCode, repeatCount, event);
}
public void DisplayToast(String str)
{
Log.i("out", str);
}
}
[/code] |
|