我想检查一下设备上的硬回键是否只在我的上下文菜单中按下,这样我就可以运行一个意图来重启活动。谢谢。到目前为止,我的代码是
@Override
public boolean onCreateOptionsMenu(Menu menu) {
item = menu.add("Something");
item.setIcon(R.drawable.ic_menu_something);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//search
if (item.getTitle() == "Something") {
//how do I check if hard back key was pressed inside this IF statement as to run below intent
Intent myCustomView = new Intent(this, com.example.something.class);
startActivityForResult(myCustomView, RESULT_OK);
}
答案 0 :(得分:0)
在我为您加载喷枪之前,通常不鼓励改变硬件按键的功能。用户期望某些行为,不应该被搞乱。
也就是说,您可以捕获onKeyDown事件并测试以查看Back键是否是按下的键:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//do your thing
return true;
}
return super.onKeyDown(keyCode, event);
}