如何保存在警报对话框中选择了哪个按钮,以便它不再显示?

how to save which button was selected in alertdialog box so that it never shows again?

我需要一些帮助,因为我正在寻找答案所有结果都没有帮助我只想直接复制和粘贴代码,这样我就可以保存选择的内容并且不再显示对话框。

SharedPreferences sharedpreferences = getSharedPreferences("shared", Context.MODE_PRIVATE);
            AlertDialog.Builder b=  new  AlertDialog.Builder(this)
                    .setTitle("Review and Rate Converjz!")
                    .setMessage("Have you enjoyed Converjz? If you wouldn't mind to take a minute of your time and rate/review this app," +
                            "that would be much appreciated!")
                    .setPositiveButton("RATE/REVIEW", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.TechnologyForTomorrow.Converjz");
                                    Intent openPlayStore = new Intent(Intent.ACTION_VIEW, uri);
                                    try {
                                        startActivity(openPlayStore);
                                    } catch (ActivityNotFoundException e) {

                                    }
                                dialog.dismiss();


                                }


                            }

                    )
                    .setNegativeButton("CANCEL",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int whichButton) {
                                    dialog.dismiss();

                                }
                            }
                    );
            b.show();


        }

这是您问题的答案,*直截了当

https://github.com/danialgoodwin/android--one-time-alert-dialog

使用下面的代码存储用户在应用程序首选项中选择的密钥。

private static final String settingsKey = "MyAppSettings";
private static final String hasUserRatedAppKey= "hasUserRatedApp";
public static boolean hasUserRatedApp(Context act)
{
    SharedPreferences pref = act.getSharedPreferences(settingsKey, Context.MODE_PRIVATE);
    return pref.getBoolean(hasUserRatedAppKey, false);
}

public static void setHasUserRated(Context context, boolean rated)
{
    SharedPreferences pref = act.getSharedPreferences(settingsKey, Context.MODE_PRIVATE);
    SharedPreferences.Editor preferencesEditor = pref.edit();
    preferencesEditor.putBoolean(hasUserRatedAppKey, rated);
    preferencesEditor.commit();
}

并在 setPositiveButton 中设置 rated 的值:

.setPositiveButton("RATE/REVIEW", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ....
                            setHasUserRated(getContext(),true);
                            dialog.dismiss();


                            }


                        }

                )
                ....

编辑:我漏掉了一部分:

在显示警告对话框之前,您必须检查之前是否对应用进行过评级,这就是我包含 hasUserRatedApp 函数的原因:

if(!hasUserRatedApp(MyActivityClass.this))
{
    AlertDialog.Builder b= ...
}