1
我試圖使用listPreference更改活動的背景顏色。我寫了一些代碼,但它直接到了黃色。使用ListPreference更改背景顏色
這裏是字符串數組:
<string-array name="backgroundColors">
<item name="1">Red</item>
<item name="2">Green</item>
<item name="3">Blue</item>
<item name="4">Yellow</item>
</string-array>
<string-array name="backgroundColorEntries">
<item name="1">#FF0000</item>
<item name="2">#00FF00</item>
<item name="3">#0000FF</item>
<item name="4">#FFFF00</item>
</string-array>
這裏就是我在XML文件中的上榜:
<ListPreference
android:key="prefSetBackground"
android:entries="@array/backgroundColors"
android:summary="Set the background color of the main page."
android:entryValues="@array/backgroundColorEntries"
android:title="@string/pref_1" />
最後,這裏是Java的一部分:
@Override
public void onWindowFocusChanged(boolean hasFocus) {
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String background_chooser = getPrefs
.getString("prefSetBackground", "0");
View view = findViewById(R.id.main_layout);
if (background_chooser.equals("1")) {
view.setBackgroundColor(Color.RED);
} else if (background_chooser.equals("2")) {
view.setBackgroundColor(Color.GREEN);
} else if (background_chooser.equals("3")) {
view.setBackgroundColor(Color.BLUE);
} else {
view.setBackgroundColor(Color.YELLOW);
}
super.onWindowFocusChanged(hasFocus);
}
我想我在這裏做錯了什麼,但我不知道是什麼。我是使用ListPreferences的新手,請耐心等待。
在此先感謝。
編輯:從我能告訴在我的手機上運行的應用程序結束時,它的默認最後一部分意味着紅色,藍色和綠色返回false。背景顏色變黃。當我改變偏好時,沒有任何反應。
編輯#2:似乎列表首選項和Java不鏈接在一起。我不知道我做錯了什麼。
結果都是一樣的。 –
好的,更改了XML並沒有發生任何事情。當我更改列表首選項時,它仍然保持黃色,沒有任何事情發生。 –
請在日誌中打印'background_chooser'並檢查您得到的值。 –