我的应用程序上有此代码
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent a = new Intent(this,a_stages.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
return true;
}
return super.onKeyDown(keyCode, event);
}
现在我想添加一个警告对话框,询问用户他/她是否想去,例如在另一个页面上,如果用户点击是,它将意图进入特定页面,如果用户点击取消,对话框。取消();。
编辑 我尝试了这段代码,但在“Intent a = new Intent(this,a_stages.class);”行出现错误 上面写着“构造函数 Intent(new DialogInterface.OnClickListener(){}, Class) 未定义”
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(bq1.this);
// Setting Dialog Title
alertDialog.setTitle("Go back to home");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want to go back to home?");
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent a = new Intent(this,a_stages.class);
a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(a);
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
return super.onKeyDown(keyCode, event);
}