我开发了一款可成功运行的应用程序,但有两项活动,第一项是启动屏幕,第二项是webview。现在的问题是,当后退按钮被按下时,它会关闭应用程序并退出,但是后退,前进和刷新按钮在应用程序中用于内部导航。所以如何显示你确定要退出对话框或防止用户停用后退键。如何防止用户在按下后退按钮时退出应用程序
3
A
回答
0
覆盖在你的活动
@Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Signout your app");
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
0
onBackPressed通过覆盖Activity
类onBackPressed()
方法试试这个办法
public class YourActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
int imageResource = android.R.drawable.ic_dialog_alert;
Drawable image = getResources().getDrawable(imageResource);
builder.setTitle("Exit").setMessage("want to exit?").setIcon(image).setCancelable(false).setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
}).setNegativeButton("no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.setCancelable(false);
alert.show();
}
}
9
事情是这样的:
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
MyActivity.super.onBackPressed();
}
})
.setNegativeButton("No", null)
.show();
}
0
只要做,
@Override
public void onBackPressed() {
// super.onBackPressed();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
// Setting Dialog Title
alertDialog.setTitle("Exit alert");
alertDialog.setMessage("Do You want Exit??");
alertDialog .setIcon(R.drawable.galleryalart);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
[youractivity].super.onBackPressed();
}
});
alertDialog.show();
}
相关问题
- 1. 制表符和后退按钮。如何防止用户退出应用程序
- 2. Ionic 2 - 防止退出应用程序硬件后退按钮
- 3. 当按下后退按钮时退出应用程序
- 4. 防止退出按钮退出科尔多瓦应用程序
- 5. Android如何防止后退按钮退出应用程序(按回家按钮后)
- 6. 当用户按下后退按钮时,应用程序崩溃
- 7. 每当我按下后退按钮时,应用程序停止
- 8. WP7 PhoneGap应用程序后退按钮退出应用程序
- 9. 在退出应用程序之前按两次后退按钮
- 10. 应用按后退按钮当按下后退按钮
- 11. 如何在按下硬件后退按钮android时退出应用程序退出?
- 12. 当用户按下后退按钮时停止线程
- 13. 应用程序崩溃,用户按下后退按钮
- 14. 防止后退按钮
- 15. 如何在后退按钮上按下应用程序
- 16. 退出应用程序后退按键
- 17. 退出应用程序上后退按钮按不工作
- 18. 按下退出按钮后停止GPS
- 19. 在Android中按下home按钮时退出应用程序
- 20. 在退出按钮时退出Phonegap-Android测试应用程序?
- 21. 防止后退按钮关闭我的应用程序
- 22. 退出应用程序时,当按下
- 23. 当用户点击后退按钮退出应用程序时显示警告
- 24. Android - 按下后退按钮时如何退出活动?
- 25. DispatchSource.makeTimerSource当按下后退按钮时崩溃应用程序Swift
- 26. 按下后退按钮时的应用程序关闭
- 27. 按下后退按钮时Android应用程序崩溃
- 28. 当我按下后退按钮时,应用程序崩溃
- 29. 按下后退按钮时的Android关闭应用程序
- 30. AngularJS当用户按下后退按钮
谢谢大家回答。 –