Android 我怎样才能把“我”写进去;您确定要退出吗?“;当我按下后退按钮时
当我按下“后退”按钮时,我想在我的应用程序中输入一条消息“你确定要退出吗?”和“是”-“否”。这是可能的吗?我该怎么做呢?谢谢在中添加用于警报对话的代码片段Android 我怎样才能把“我”写进去;您确定要退出吗?“;当我按下后退按钮时,android,back,Android,Back,当我按下“后退”按钮时,我想在我的应用程序中输入一条消息“你确定要退出吗?”和“是”-“否”。这是可能的吗?我该怎么做呢?谢谢在中添加用于警报对话的代码片段 @Override public void onBackPressed() { final AlertDialog alert = new AlertDialog.Builder(new ContextThemeWrapper(contextForAlert,R.style.CustomAlertDialogue)).cre
@Override
public void onBackPressed() {
final AlertDialog alert = new AlertDialog.Builder(new ContextThemeWrapper(contextForAlert,R.style.CustomAlertDialogue)).create();
alert.setTitle(title);
alert.setMessage(message);
alert.setIcon(R.drawable.warning_icon);
alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
alert.show();
}
摘自答案。是的,可以通过活动后按alertdialog
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ExampleActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}
我有两个滑动标签。我把它放在主活动或两个标签中?@gunaselan谢谢:)但是如果我在背面按“是”,在我重新打开应用程序后,按钮就不起作用了。为什么?如果你在onClick外有
super.onBackPressed()
,请将其删除。谢谢。这对我很有用。这也是一个简单的解决方案。我们可以使用自定义的警报框,以获得良好的ui外观
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ExampleActivity.this.finish();
}
})
.setNegativeButton("No", null)
.show();
}