警报对话框首选项不保存

时间:2012-07-25 20:53:17

标签: java android dialog alert android-preferences

所以我会弹出这个AlertDialog并在用户首次打开应用时说出正确的话。

然后他们可以选择点击CheckBox,并在下次启动应用时停用该对话框。

启用CheckBox无效。当应用程序完全关闭然后重新启动时,它会再次弹出警告对话框。有没有人看到我做的事情有什么问题?

 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>

2 个答案:

答案 0 :(得分:3)

我看到的问题是你不应该在字符串上使用==或!=。相反,使用.equals,所以它是

if( !skipMessage.equals("checked") )

答案 1 :(得分:2)

如果首选skipMessage被“选中”,则唯一不会发生的事情是将图标设置为R.drawable.icon。将adb.show()放入if语句中(用大括号括起来)。

if (!skipMessage.equals("checked") ) {
    adb.show();
}