Java 未检测到KeyEvent.KEYCODE\u DPAD\u中心
我在关注谷歌的 关于检测D-pad按键事件的指南,但它们似乎不起作用 以下是我的部分代码:Java 未检测到KeyEvent.KEYCODE\u DPAD\u中心,java,android,google-glass,google-gdk,Java,Android,Google Glass,Google Gdk,我在关注谷歌的 关于检测D-pad按键事件的指南,但它们似乎不起作用 以下是我的部分代码: public final class ResultActivity extends Activity implements AsyncResponse{ [...] @Override public boolean onKeyUp(int keycode, KeyEvent event){ if(keycode == KeyEvent.K
public final class ResultActivity extends Activity implements AsyncResponse{
[...]
@Override
public boolean onKeyUp(int keycode, KeyEvent event){
if(keycode == KeyEvent.KEYCODE_DPAD_DOWN){
//this doesnt get detected
return true;
}
if(keycode == 4){
//this gets detected
return true;
}
return false;
}//end onKeyUp
}//end activity
是的,我也试过按键盘键
是什么导致了这个问题?这并不能完全回答你的问题,但我认为这是一个很好的建议。当我试图以这种方式检测
keyevent
s时,我也遇到了同样的问题。我只能正确注册其中两个事件。在玩了几天之后,我决定寻找其他的选择,并找到了手势检测器
,尝试了它,它工作得非常好。您可以找到手势检测器
的。我还将自己给你一些代码来帮助你
这是我在代码中使用手势检测器的方式。在最上面,我声明了一个私有的手势检测器
:
private GestureDetector gestureDetector;
然后,在onCreate()
方法中,我调用了一个创建GestureDetector
的方法(与仅在onCreate()
方法中创建它相比,没有理由这样做-这样看起来更干净,更有条理):
createGestureDetector()
方法如下所示:
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
//we are creating our gesture detector here
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) { //onTap
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(SoundEffectConstants.CLICK); //if we tap once, play a click sound
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
return false; //this is the wrong kind of scroll
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { //fling = a single slide
int dx = (int) (motionEvent2.getX() - motionEvent.getX()); //figure out the distance moved horizontally
int dy = (int) (motionEvent2.getY() - motionEvent.getY()); //figure out the distance moved vertically
//if dx > 0, moved forward, if dx < 0, moved backward
//if dy > 0, moved up, if dy < 0, moved down
return true;
}
});
return gestureDetectorTemp;
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onTouchEvent(event);
}
return false;
}
专用GestureDetector createGestureDetector(上下文){
GestureDetector gestureDetectorTemp=新的GestureDetector(上下文,new GestureDetector.OnGetureListener()){
//我们正在这里创建我们的手势检测器
@凌驾
公共布尔onDown(MotionEvent MotionEvent){
返回false;
}
@凌驾
在ShowPress上公开作废(运动事件){
}
@凌驾
公共布尔onSingleTapUp(MotionEvent MotionEvent){//onTap
AudioManager am=(AudioManager)getSystemService(Context.AUDIO\u服务);
am.playSoundEffect(SoundEffectConstants.CLICK);//如果我们点击一次,播放一个CLICK声音
返回false;
}
@凌驾
公共布尔onScroll(MotionEvent、MotionEvent、motionEvent2、浮点距离X、浮点距离Y){
return false;//这是错误的滚动类型
}
@凌驾
public void onLongPress(运动事件){
}
@凌驾
公共布尔onFling(MotionEvent、MotionEvent、motionEvent2、float v、float v2){//fling=单个幻灯片
int dx=(int)(motionEvent2.getX()-motionEvent.getX());//计算水平移动的距离
int dy=(int)(motionEvent2.getY()-motionEvent.getY());//计算垂直移动的距离
//如果dx>0,则向前移动;如果dx<0,则向后移动
//如果dy>0,则向上移动;如果dy<0,则向下移动
返回true;
}
});
返回手势detectortemp;
}
@凌驾
公共布尔onGenericMotionEvent(运动事件){
if(手势检测器!=null){
返回gestureDetector.onTouchEvent(事件);
}
返回false;
}
您必须确保包含我在末尾使用的onGenericMotionEvent()
方法。这就是确保每次发生运动事件时都会通知您的手势检测器的原因
最后要注意的一点是gesturedetator
中的返回-返回true以使用事件,或返回false以不使用事件。换句话说,如果您想覆盖默认情况下发生的操作,例如在向下滑动时关闭应用程序,只需进入onFling()
事件,如果dy<0,则返回true。这会消耗事件,而不会将其发送给其他默认方法来处理 这并不能完全回答你的问题,但我认为这是一个很好的建议。当我试图以这种方式检测keyevent
s时,我也遇到了同样的问题。我只能正确注册其中两个事件。在玩了几天之后,我决定寻找其他的选择,并找到了手势检测器
,尝试了它,它工作得非常好。您可以找到手势检测器
的。我还将自己给你一些代码来帮助你
这是我在代码中使用手势检测器的方式。在最上面,我声明了一个私有的手势检测器
:
private GestureDetector gestureDetector;
然后,在onCreate()
方法中,我调用了一个创建GestureDetector
的方法(与仅在onCreate()
方法中创建它相比,没有理由这样做-这样看起来更干净,更有条理):
createGestureDetector()
方法如下所示:
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
//we are creating our gesture detector here
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) { //onTap
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(SoundEffectConstants.CLICK); //if we tap once, play a click sound
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
return false; //this is the wrong kind of scroll
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { //fling = a single slide
int dx = (int) (motionEvent2.getX() - motionEvent.getX()); //figure out the distance moved horizontally
int dy = (int) (motionEvent2.getY() - motionEvent.getY()); //figure out the distance moved vertically
//if dx > 0, moved forward, if dx < 0, moved backward
//if dy > 0, moved up, if dy < 0, moved down
return true;
}
});
return gestureDetectorTemp;
}
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onTouchEvent(event);
}
return false;
}
专用GestureDetector createGestureDetector(上下文){
GestureDetector gestureDetectorTemp=新的GestureDetector(上下文,new GestureDetector.OnGetureListener()){
//我们正在这里创建我们的手势检测器
@凌驾
公共布尔onDown(MotionEvent MotionEvent){
返回false;
}
@凌驾
在ShowPress上公开作废(运动事件){
}
@凌驾
公共布尔onSingleTapUp(MotionEvent MotionEvent){//onTap
AudioManager am=(AudioManager)getSystemService(Context.AUDIO\u服务);
am.playSoundEffect(SoundEffectConstants.CLICK);//如果我们点击一次,播放一个CLICK声音
返回false;
}
@凌驾
公共布尔onScroll(MotionEvent、MotionEvent、motionEvent2、浮点距离X、浮点距离Y){
return false;//这是错误的滚动类型
}
@凌驾
public void onLongPress(运动事件){
}
@凌驾
公共布尔onFling(MotionEvent、MotionEvent、motionEvent2、float v、float v2){//fling=单个幻灯片
int dx=(int)(motionEvent2.getX()-motionEve