onKey onKeyDown无效

时间:2016-09-15 18:07:35

标签: android

我试图在我的Android应用中注册方向键盘/电视遥控器d pad点击。我目前正在使用Android模拟器进行测试,我试图在额外的设置菜单下点击方向键输入。但我不确定为什么这不起作用 - 任何帮助都会受到赞赏

public class FullscreenActivity extends AppCompatActivity {

    private View mContentView;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_fullscreen);

            mContentView = findViewById(R.id.fullscreen_content);

            mContentView.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    Log.d("debug", "we are here");
                    if (event.getAction() == KeyEvent.ACTION_DOWN)
                        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                            Log.d("keycode", "center pressed");
                            return true;
                        }
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_DPAD_UP:
                        case KeyEvent.KEYCODE_DPAD_DOWN:
                        case KeyEvent.KEYCODE_DPAD_RIGHT:
                        case KeyEvent.KEYCODE_DPAD_LEFT:
                        case KeyEvent.KEYCODE_DPAD_CENTER:
                            Log.d("OnKey", "key pressed!");
                            return true;
                    }
                    return false;
                }
            });
        }
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            return super.onKeyDown(keyCode, event);
        }
}

运行此代码在logcat

上没有任何输出

2 个答案:

答案 0 :(得分:1)

尝试使用此代码

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(keyCode==KeyEvent.KeyEvent.KEYCODE_DPAD_CENTER)
    {
        Toast.makeText(<Activity_name>.this, "Application Quits", Toast.LENGTH_SHORT).show();
          finish();
          return false;
    }

    return super.onKeyDown(keyCode, event);
}

答案 1 :(得分:1)

您可以覆盖活动的onKeyDown()方法以检测按键。

试试这个

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (event.getAction() == KeyEvent.ACTION_DOWN) {

        switch (event.getKeyCode()) {
            case  KeyEvent.KEYCODE_DPAD_UP:
                 showToast("UP pressed");
            return true;
            case  KeyEvent.KEYCODE_DPAD_DOWN:
                 showToast("DOWN pressed");
            return true;
            case  KeyEvent.KEYCODE_DPAD_RIGHT:
                 showToast("RIGHT pressed");
            return true;
            case  KeyEvent.KEYCODE_DPAD_LEFT:
                 showToast("LEFT pressed");
            return true;
            case  KeyEvent.KEYCODE_DPAD_CENTER:
                 showToast("CENTER pressed");
            return true;
           }
         }
        return super.onKeyDown(keyCode, event);
  }

  void showToast(String msg)
  {
    Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_SHORT).show();
  }