2012-07-25 7 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

답변

3

문제는 내가 == 또는! = 문자열을 사용해서는 안된다는 것입니다. 대신 .equals를 사용하십시오.

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

Thanks @Alex Coleman이 문제가 해결되었습니다. –

2

선호 skipMessage가 R.drawable.icon의 아이콘을 설정하는 것입니다 "확인"경우에 발생하지 않는 유일한 것입니다. adb.show()도 if 문에 넣습니다 (중괄호로 묶습니다).

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

들여 쓰기에 대해 유감스럽게 생각합니다. –

+0

당신이 그걸 어떻게 보일까,이게'if (skipMessage! = ("checked") adb.show())' –

+0

내 편집을 참조하십시오. @Alex도 맞습니다. =를 사용하여 문자열을 비교할 수는 없습니다. 그리고 값을 확인하면 경고 대화 상자를 작성하는 것을 정말로 신경 쓰지 않아도됩니다. 모든 코드를 if 문의 중괄호 (또는 더 나은 방법으로 리팩터링)에 넣을 수 있습니다. – Magicode

관련 문제