java - 警报对话框首选项未保存

标签 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>

最佳答案

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

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

关于java - 警报对话框首选项未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658429/

相关文章:

java - Android应用在启动时崩溃

c# - 对话框无法通过 .Show 正确显示,但又不想在 C# 中进行多线程处理时阻塞 .ShowDialog

java - 修改元模型的架构以更改/重命名列名称

android - 如何: Load hdpi resource into mdpi system

javascript - 如何从 MyApp Android 打开网络浏览器应用程序的 URL

android - 从应用程序内启动浏览器 - 如何返回应用程序?

Android:在警告对话框中格式化字体

dialog - 当用户退出 xe :dialog 时捕获

java - 如何使用 Java 中的 Apache poi 创建指向其他工作表上的过滤器的超链接?

java - neo4j 中的 GraphDatabaseService 和 NeoService 有什么区别