0

だから私はAlertDialog、ユーザーが最初にアプリを開いたときにポップアップして正しいことを言うこれを持っています.

そして、次にアプリを起動したときに、a をクリック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();

   }

以下は私のAlertDialogXMLです

<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>
4

2 に答える 2

3

私が目にする問題は、文字列で == または != を使用してはならないということです。代わりに、.equals を使用してください。

if( !skipMessage.equals("checked") )
于 2012-07-25T21:03:55.777 に答える
2

skipMessage設定が「チェック」されている場合に起こらない唯一のことは、アイコンを R.drawable.icon に設定することです。を if ステートメントにも入れadb.show()ます (中かっこで囲みます)。

if (!skipMessage.equals("checked") ) {
    adb.show();
}
于 2012-07-25T20:58:32.477 に答える