Android onkeydown()无法正常工作

时间:2013-12-06 13:04:36

标签: android onkeydown

我有一个活动,我会在方向改变时标记全屏,问题是当你当前处于全屏状态时按下后退按钮它会全屏返回上一个片段,我这样做不想。我添加了这段代码,但它似乎没有被称为:

public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
    //do what ever you want here
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    getActivity().getActionBar().show();
    getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    Toast.makeText(getActivity().getBaseContext(), "backbuttonpressed", Toast.LENGTH_LONG).show();
    getActivity().finish();
    return true;
  }
  return super.getActivity().onKeyDown(keyCode, event);
}

1 个答案:

答案 0 :(得分:0)

也许这对你有用:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    Setting s = Utils.readSettings(this);
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}