2013-11-27 512 views
0

嗨,任何人都可以幫助我,請讓我很難做出這項工作,並一直在嘗試很多方法來使這項工作,但每次我運行此代碼它只顯示beginner1Check,即使我打開布爾爲真。任何人都可以幫助我嗎?沒有正確加載按鈕

這裏是我的代碼

的.java

public void initialize(){ 

    SharedPreferences beginner1Prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
    boolean beginner1_pref = beginner1Prefs.getBoolean("Beginner1", false); 

    if (beginner1_pref == false){ 
     beginner1Check = (Button) findViewById(R.id.btnBeginner1Check); 
     beginner1Check.setOnClickListener(myOnlyhandler); 
    } else if (beginner1_pref == true){ 
     beginner1 = (Button) findViewById(R.id.btnBeginner1); 
     beginner1.setOnClickListener(myOnlyhandler); 
    } 

    btnLogo = (Button) findViewById(R.id.btnLogo); 
    beginner2 = (Button) findViewById(R.id.btnBeginner2); 
    beginner3 = (Button) findViewById(R.id.btnBeginner3); 
    beginner4 = (Button) findViewById(R.id.btnBeginner4); 
    beginner5 = (Button) findViewById(R.id.btnBeginner5); 
    beginner6 = (Button) findViewById(R.id.btnBeginner6); 
    beginner7 = (Button) findViewById(R.id.btnBeginner7); 
    beginner8 = (Button) findViewById(R.id.btnBeginner8); 
    beginner9 = (Button) findViewById(R.id.btnBeginner9); 


    btnLogo.setOnClickListener(myOnlyhandler); 
    beginner2.setOnClickListener(myOnlyhandler); 
    beginner3.setOnClickListener(myOnlyhandler); 
    beginner4.setOnClickListener(myOnlyhandler); 
    beginner5.setOnClickListener(myOnlyhandler); 
    beginner6.setOnClickListener(myOnlyhandler); 
    beginner7.setOnClickListener(myOnlyhandler); 
    beginner8.setOnClickListener(myOnlyhandler); 
    beginner9.setOnClickListener(myOnlyhandler); 

} 

XML>的prefs.xml

<CheckBoxPreference 
    android:title="Beginner1" 
    android:defaultValue="false" 
    android:key="Beginner1" 
    android:summary="Beginner1" /> 
+0

你想做什麼?提供更多細節。 – Naddy

+0

我想加載button1,如果beginner1_pref = true,然後隱藏button2然後在beginner1_pref = false顯示button2然後隱藏button1 – John

+0

給你的'活動'代碼。 – Naddy

回答

0

的問題是,你還沒有第二Button無形。您剛剛初始化了第一個Button,但您的第二個Button顯然存在於您的佈局中。它不會隱藏,除非你明確隱藏它。修改你的代碼是這樣的:

if (beginner1_pref == false){ 
     beginner1Check = (Button) findViewById(R.id.btnBeginner1Check); 
     beginner1 = (Button) findViewById(R.id.btnBeginner1); 
     beginner1.setVisibility(View.INVISIBLE);//You can replace it with VIEW.GONE depending on your needs. 
     beginner1Check.setOnClickListener(myOnlyhandler); 
    } else if (beginner1_pref == true){ 
     beginner1 = (Button) findViewById(R.id.btnBeginner1); 
     beginner1Check = (Button) findViewById(R.id.btnBeginner1Check); 
     beginner1Check.setVisibility(View.INVISIBLE); 
     beginner1.setOnClickListener(myOnlyhandler); 
    } 
+0

@Naddy請不要告訴人們他們必須接受答案。 *這根本不是真的。* –