Android 如何保存在alertdialog box中选择的按钮,使其不再显示?
我需要一些帮助,因为我正在搜索一个答案,所有的结果都没有帮助,我只想直接复制和粘贴代码,这样我就可以保存所选内容,不再显示对话框Android 如何保存在alertdialog box中选择的按钮,使其不再显示?,android,sharedpreferences,android-alertdialog,rating-system,Android,Sharedpreferences,Android Alertdialog,Rating System,我需要一些帮助,因为我正在搜索一个答案,所有的结果都没有帮助,我只想直接复制和粘贴代码,这样我就可以保存所选内容,不再显示对话框 SharedPreferences sharedpreferences = getSharedPreferences("shared", Context.MODE_PRIVATE); AlertDialog.Builder b= new AlertDialog.Builder(this) .setTi
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();
}
这是您问题的答案,*直截了当
这是你问题的答案,*直截了当
使用下面的代码存储用户在应用程序首选项中选择的密钥
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中设置额定值:
.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= ...
}
使用下面的代码存储用户在应用程序首选项中选择的密钥
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中设置额定值:
.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= ...
}
它无法解析getContext()方法,我将setHasUserRated()方法中的act更改为Context,只是使用了alertdialog b.getContext,并在它说b可能尚未初始化后将其更改为final。请帮助是否在活动类或片段中使用它?如果在活动中使用它,请使用MyActivityClass.this而不是getContext()。将MyActivityClass替换为您的活动名称。我在手机上测试过,重新启动活动或整个应用程序后,该对话框再次显示。现在仍在工作。它无法解析getContext()方法,我将setHasUserRated()方法中的act更改为Context只是使用了alertdialog b.getContext,并在它说b可能尚未初始化后将其更改为final。请帮助是否在活动类或片段中使用它?如果在活动中使用它,请使用MyActivityClass.this而不是getContext()。将MyActivityClass替换为您的活动名称。我在手机上测试过,重新启动活动或整个应用程序后,该对话框再次显示。现在还在工作。