Java 警报对话框首选项未保存
因此,我有一个Java 警报对话框首选项未保存,java,android,dialog,alert,android-preferences,Java,Android,Dialog,Alert,Android Preferences,因此,我有一个AlertDialog,当用户第一次打开应用程序时,它会弹出并正确地说一些话 然后他们可以选择单击复选框,并在下次应用程序启动时禁用弹出的对话框 启用复选框无法正常工作。当应用程序完全关闭然后重新启动时,它会再次弹出警报对话框。有人认为我所做的有什么不对吗 public static final String PREFS_NAME = "MyPrefsFile1"; public CheckBox dontShowAgain; @Override public void
AlertDialog
,当用户第一次打开应用程序时,它会弹出并正确地说一些话
然后他们可以选择单击复选框
,并在下次应用程序启动时禁用弹出的对话框
启用复选框
无法正常工作。当应用程序完全关闭然后重新启动时,它会再次弹出警报对话框。有人认为我所做的有什么不对吗
public static final String PREFS_NAME = "MyPrefsFile1";
public CheckBox dontShowAgain;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder adb = new AlertDialog.Builder(this);
LayoutInflater adbInflater = LayoutInflater.from(this);
View eulaLayout = adbInflater.inflate(R.layout.custom_dialog, null);
dontShowAgain = (CheckBox)eulaLayout.findViewById(R.id.checkBox1);
adb.setView(eulaLayout);
adb.setTitle("Alert Dialog");
adb.setMessage("My Customer Alert Dialog Message Body");
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
//CheckBox Confirm for Alert Dialog
public void onClick(DialogInterface dialog, int which) {
String checkBoxResult = "NOT checked";
if (dontShowAgain.isChecked()) checkBoxResult = "checked";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("skipMessage", checkBoxResult);
// Commit the edits!
editor.commit();
return;
}
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Action for 'Cancel' Button
String checkBoxResult = "NOT checked";
if (dontShowAgain.isChecked()) checkBoxResult = "checked";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("skipMessage", checkBoxResult);
// Commit the edits!
editor.commit();
return;
}
});
//Preferences For Alert Dialog
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String skipMessage = settings.getString("skipMessage", "NOT checked");
if (skipMessage !=("checked") )
adb.setIcon(R.drawable.icon);
adb.show();
}
下面是我的AlertDialog
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
android:textSize="5px" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do Not Show Me This Again" />
</LinearLayout>
如果首选项
skipMessage
为“选中”,则唯一不会发生的事情是将图标设置为R.drawable.icon。将adb.show()
也放在if语句中(用花括号括起来)
如果首选项
skipMessage
为“选中”,则唯一不会发生的事情是将图标设置为R.drawable.icon。将adb.show()
也放在if语句中(用花括号括起来)
我看到的问题是,您不应该使用==或!=在弦上。相反,请使用.equals,这样就可以了
if( !skipMessage.equals("checked") )
我看到的问题是,您不应该使用==或!=在弦上。相反,请使用.equals,这样就可以了
if( !skipMessage.equals("checked") )
请你把凹痕清理一下好吗?这很难解决。另外,当你说它不工作时,是不是有错误或者只是缺少结果?请你清理一下缩进好吗?这很难解决。另外,当你说它不起作用时,是有错误还是缺少结果?伙计们,我已经把它清除了,很抱歉缩进,希望它更容易阅读。你能参考一下它的样子吗,如果(skipMessage!=(“checked”)adb.show()看到我的编辑@亚历克斯也是对的,你不能用比较字符串。如果选中该值,实际上根本不需要构建警报对话框。你可以把所有的代码放在if语句的方括号内(或者用更好的方式重构它)。你能告诉我编辑上面的代码是什么意思吗?如果这不太重要的话,我很难理解你的意思。我理解,如果复选框是createthealert对话框,那么您的意思是将代码放在花括号内,或者如果是station,您可以写一个小例子吗。顺便说一句,你的和@Alex确实解决了我的首选项保存问题。谢谢各位,我已经把它清理干净了,很抱歉缩进,希望它更容易阅读。如果(skipMessage!=(“checked”)adb.show())看到我的编辑,你能参考一下它的样子吗@亚历克斯也是对的,你不能用比较字符串。如果选中该值,实际上根本不需要构建警报对话框。你可以把所有的代码放在if语句的方括号内(或者用更好的方式重构它)。你能告诉我编辑上面的代码是什么意思吗?如果这不太重要的话,我很难理解你的意思。我理解,如果复选框是createthealert对话框,那么您的意思是将代码放在花括号内,或者如果是station,您可以写一个小例子吗。顺便说一句,你的和@Alex确实解决了我的首选项保存问题。谢谢