我有一个应用程序,当用户在设置中选择颜色时,我想更改应用程序的ColorPrimary?我怎么能这样做?
答案 0 :(得分:0)
在设置中选择颜色时调用此方法
void onBackgroundColor(String color){
switch(color){
case "red":
sSavePreferences(getApplicationContext(), "color", "blue");
setBackground(color);
break;
case "blue":
sSavePreferences(getApplicationContext(), "color", "blue");
setBackground(color);
break;
case "green":
sSavePreferences(getApplicationContext(), "color", "blue");
setBackground(color);
break;
default:break;
}
}
void setBackground(String color){
//code to change background color
}
这是使用SharedPreferences
保存设置的方法public static void sSavePreferences(Context context, String key, String value) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
OnCreate of Activity 您可以使用此方法加载以前的颜色
setBackground(sLoadSavedPreferencesString(getApplicationContext(), "color"));
用于加载颜色:
public static String sLoadSavedPreferencesString(Context context, String key) {
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(context);
String value = sharedPreferences.getString(key, "No Name Found");
return value;
}