如何保存在alertdialog框中选择了哪个按钮,以便它再也不显示?

时间:2016-01-19 13:39:45

标签: android sharedpreferences alertdialog rating-system

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

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();


        }

2 个答案:

答案 0 :(得分:3)

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

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中设置rating的值:

.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= ...
}

答案 1 :(得分:0)