当我强行关闭模拟器或设备上的应用程序时,我遇到了另一个与alertDialog
碰到的问题,当我转到该设备或模拟器上的应用程序再次启动我的应用程序时alertDialog
显示。我不久前在这个链接Shared Prefence for alert dialog is making my application non responsive中遇到了类似的问题,我认为我所有的问题都是一劳永逸地解决的。所以有人可以帮我解决这个问题。
final SharedPreferences settings = getSharedPreferences("pref_name", 0);
("installed", false);
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setAdapter(new MyAdapter(), null);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("installed", true);
editor.commit();
}
});
alertDialog.show();
如果您需要我详细说明,请告诉我
答案 0 :(得分:0)
final SharedPreferences settings = getSharedPreferences("pref_name", 0);
boolean dialogAlreadyShown = settings.getBoolean("installed", false);
if (dialogAlreadyShown == false) {
showTheDialogYouWannaShow();
settings.edit().putBoolean("installed", true).commit();
}
这样,下次运行此代码时,对话框将不会显示...您还可以在对话框的onClick上保存设置,但如果用户没有单击对话框上的按钮,对话框可能会多次显示,直到用户按下按钮,根据您的要求,也可以没问题。
注意:在 showTheDialogYouWannaShow ()中,我假设您正在编写代码以构建问题中已经显示的警告对话框。