2012-07-25 22 views
0

警告对话框偏好所以我有这个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> 
+0

你能清理缩进吗?这很难完成。另外,当你说它不起作用有没有错误或只是缺乏结果? – thegrinner 2012-07-25 20:55:16

回答

3

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

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

谢谢@Alex Coleman解决了这个问题。 – 2012-07-25 21:31:36

2

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

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

我已经清除了它们,对于缩进感到抱歉,希望它更容易阅读。 – 2012-07-25 20:59:38

+0

你能REFFERENCE那将是什么样子,它应该是这样的'如果(skipMessage!=(“选中”)adb.show())' – 2012-07-25 21:03:44

+0

见我的编辑。 @Alex也是对的,你不能使用!=比较字符串。如果检查了值,你真的不需要费心建立警报对话框。您可以将所有代码放在if语句的大括号内(或者以更好的方式重构它)。 – Magicode 2012-07-25 21:25:47