当用户从他的设备上按下后退按钮时我想要一个动作。我能做到吗?谢谢
答案 0 :(得分:1)
Here是用户按下后退按钮时如何实现功能的一些示例
答案 1 :(得分:0)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
if(canPressBack) {
return super.onKeyDown(keyCode, event);
}
}
return true;
}
答案 2 :(得分:0)
活动有public void onBackPressed()
回调方法..使用它
答案 3 :(得分:0)
试试此代码
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
String dialog_message = "Please Select at least one item or press Cancel button";
if(keyCode==KeyEvent.KEYCODE_BACK)
{
AlertDialog.Builder invalid_input_dialog = new AlertDialog.Builder(Classname.this);
invalid_input_dialog.setTitle("Application name")
.setMessage(dialog_message)
.setCancelable(true)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
return true;
}
return super.onKeyDown(keyCode, event);
}