【问题标题】:Alert Dialog preference not saving警报对话框首选项未保存
【发布时间】:2012-07-25 20:53:17
【问题描述】:

所以我有这个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>

【问题讨论】:

  • 你能清理一下缩进吗?很难解决。另外,当您说它不起作用时,是有错误还是只是缺少结果?

标签: java android dialog alert android-preferences


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我已经把它清理干净了,对缩进很抱歉,希望它更容易阅读。
    • 你能参考一下那会是什么样子吗,应该是这样的if (skipMessage !=("checked") adb.show() )
    • 查看我的编辑。 @Alex 也是对的,你不能使用 != 来比较字符串。如果检查了该值,您实际上根本不需要构建警报对话框。您可以将所有这些代码放在 if 语句的大括号内(或以更好的方式对其进行重构)。
    • 如果没有太多要求,您能否通过编辑上面的代码向我展示您的意思,我无法理解您的意思。我知道如果通过创建警报对话框来检查该框,但是您的意思是将代码放在大括号内,或者如果声明,您可以写一个小例子。顺便说一句,你和@Alex 确实解决了我保存偏好的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 2013-06-26
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多