我開發了一款可成功運行的應用程序,但有兩項活動,第一項是啓動屏幕,第二項是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. 按下退出按鈕後停止GPS
- 17. 在退出按鈕時退出Phonegap-Android測試應用程序?
- 18. 防止後退按鈕關閉我的應用程序
- 19. 退出應用程序時,當按下
- 20. 當用戶點擊後退按鈕退出應用程序時顯示警告
- 21. Android - 按下後退按鈕時如何退出活動?
- 22. DispatchSource.makeTimerSource當按下後退按鈕時崩潰應用程序Swift
- 23. 按下後退按鈕時的Android關閉應用程序
- 24. 按下後退按鈕時Android應用程序崩潰
- 25. AngularJS當用戶按下後退按鈕
- 26. 按下按鈕時應用程序退出
- 27. 按下主頁按鈕時退出應用程序。
- 28. 如何退出android應用程序退出按鈕?
- 29. 按兩次退出按鈕退出AIR安卓應用程序
- 30. 按兩次退出按鈕退出應用程序
謝謝大家回答。 –