0
如何通過根據用戶選擇如何通過根據用戶選擇點擊單選按鈕,更改應用程序的主題
如何通過根據用戶選擇如何通過根據用戶選擇點擊單選按鈕,更改應用程序的主題
//Pseudo code snippet is given
Let me assume the first page of android app might be having different set of radio as blue,green, red etc.,
each color might have defined in the style.xml with the required background,foreground,font color etc.,
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
<RadioButton android:id="@+id/radio_blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pirates" android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ninjas" android:onClick="onRadioButtonClicked"/>
</RadioGroup>
//In utility method
public static void savePreferences(String key, String value, Context context) {
SharedPreferences SharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = SharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
//in that page radiobuttononchange
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.blue:
if (checked)
//store the value in sharedpreferences
savePreferences("preference","blue",getcontext());
break;
case R.id.red:
if (checked)
savePreferences("preference","red",getcontext());
break;
Intent nextScreen = new Intent(
context,
Form.class);
startActivity(nextScreen);
}
}
//In next activity
//Oncreate
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String themeName = pref.getString("preference", "Theme1");
if (themeName.equals("blue")) {
setTheme(R.style.blue);
} else if (themeName.equals("red")) {
setTheme(R.style.red);
}.....
setContentView(R.layout.activity_some_layout);
}
點擊單選按鈕來更改應用程序的主題,標題欄顏色和背景顏色爲所有活動