问题
Hello I am trying to save a theme with shared preferences. When the user clicks a button with a certain theme I want that theme to be set as default and saved so when they reopen the app its still that new theme.
Main Activity:
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button1:
Utils.changeToTheme(this, Utils.THEME_DEFAULT);
break;
case R.id.button2:
Utils.changeToTheme(this, Utils.THEME_WHITE);
break;
case R.id.button3:
Utils.changeToTheme(this, Utils.THEME_BLUE);
break;
}
}
}
Utils:
public class Utils{
private static int sTheme;
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
/**
* Set the theme of the Activity, and restart it by creating a new Activity of the same type.
*/
public static void changeToTheme(Activity activity, int theme)
{
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
/** Set the theme of the activity, according to the configuration.
* @param activity*/
public static void onActivityCreateSetTheme(MainActivity activity)
{
switch (sTheme)
{
default:
case THEME_DEFAULT:
activity.setTheme(R.style.AppTheme);
break;
case THEME_WHITE:
activity.setTheme(R.style.MyTheme);
break;
case THEME_BLUE:
activity.setTheme(R.style.My2Theme);
break;
I also want the theme to be saved for all activities not just the one where the button is clicked. Thanks
回答1:
Here you can find a tutorial about SharedPreferences , basically you should store the number of the selected theme and when the app starts, check which one is stored on SharedPreferences. After retrieving the value you could "store" it globally to check the theme number on other activities (this way, you don't have to check SharedPreferences every time, just when the app starts).
UPDATE 0
Here a class that handles SharedPreference stuff:
public class SharedPreferencesManager {
/**
* SharedPreferences to store the settings. This way, they'll be available next time the user starts the app
*/
private SharedPreferences sPreferences;
/**
* Editor to make changes on sharedPreferences
*/
private SharedPreferences.Editor sEditor;
/**
* The class itself
*/
private Context context;
public SharedPreferencesManager(Context context){
this.context = context;
sPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
private SharedPreferences.Editor getEditor(){
return sPreferences.edit();
}
/**
* Store a boolean value in sharedPreferences
* @param tag identifies the value
* @param value the value itself
*/
public void storeBoolean(String tag, boolean value){
sEditor = getEditor();
sEditor.putBoolean(tag,value);
sEditor.commit();
}
/**
* Store a string in sharedPreferences
* @param tag identifies the value
* @param str the string itself
*/
public void storeString(String tag, String str){
sEditor = getEditor();
sEditor.putString(tag, str);
sEditor.commit();
}
/**
*
* @param tag identifies the value
* @param defValue default value
* @return the stored or default value
*/
public boolean retrieveBoolean(String tag, boolean defValue){
return sPreferences.getBoolean(tag,defValue);
}
/**
*
* @param tag identifies the string
* @param defStr default string
* @return the stored or default string
*/
public String retrieveString(String tag, String defStr){
return sPreferences.getString(tag, defStr);
}
/**
*
* @param tag identifies the value
* @param defValue default value
* @return the stored or default value
*/
public int retrieveInt(String tag, int defValue){
return sPreferences.getInt(tag, defValue);
}
/**
*
* @param tag identifies the value
* @param defValue the value itself
*/
public void storeInt(String tag, int defValue){
sEditor = getEditor();
sEditor.putInt(tag, defValue);
sEditor.commit();
}
//Incorrect Bracket Closing Removal.
Using this class you can store and retrieve diferent type of values on/from SharedPreferences. In your case, you'll need to store the value of a theme. You have:
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
So, after the user choose a theme you should call:
new SharedPreferencesManager(getApplicationContext()).storeInt("theme", THEME_WHITE);
You could call this function on your onClick()
handler, or where you think it's better :)
To retrieve which theme value is stored, call the below code on your onCreate method:
Utils.changeToTheme(this, new SharedPreferencesManager(this).retrieveInt("theme", THEME_WHITE));
Remember that when you want to retrieve a value from the SharedPreferences, will have to pass the default value, so if nothing related to the tag "theme" is stored, it will return the default value.
I didn't test this code, maybe you'll have to adpat it a little.
回答2:
In your Main Activity, make these changes to the onClick:
@Override
public void onClick(View v)
{
//Setup the SharedPreferences
SharedPreferences mSettings = this.getSharedPreferences("Themes", 0);
//SharedPrerefences Editor (to add content to SP)
SharedPreferences.Editor editor = mSettings.edit();
switch (v.getId())
{
case R.id.button1:
Utils.changeToTheme(this, Utils.THEME_DEFAULT);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
case R.id.button2:
Utils.changeToTheme(this, Utils.THEME_WHITE);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
case R.id.button3:
Utils.changeToTheme(this, Utils.THEME_BLUE);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
}
}
Then, at the top in your onCreate, add this prior to super and setContentView:
SharedPreferences mSettings = this.getSharedPreferences("Themes", 0);
Utils.SetTheme(mSettings.getInt("theme", 0));
What we did here was:
- Opened and stored the integers relating to the themes in SharedPreferences.
- Opened the SharedPreferences in your onCreate, then called SetTheme to look for the integer saved in the SharedPreferences and apply it to the theme.
Now when you click the button for white theme button, it will apply it.
回答3:
I've included an example that should work for you. See http://developer.android.com/guide/topics/data/data-storage.html#pref for more info. Since PREFS_NAME is public you can call getSharedPreferences(MyActivity.PREFS_NAME,0); from other activities to access the same SharedPreferences
Field in your class
public static final String PREFS_NAME = "MyPrefsFile";
put this code where you want to save the theme choice in SharedPreferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//"theme" is the key sTheme is the value you're saving
editor.putInt("theme", sTheme);
editor.commit();
And put this code where you want want to get that value back
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//"theme" is the same key 0 is the default value
int theme = settings.getInt("theme", 0);
来源:https://stackoverflow.com/questions/29112769/save-android-theme-using-shared-preferences