问题
I'm trying to save my theme by using sharedpreference, so I used this code:
My SettingsActivity:
public class SettingsActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ThemeUtils.onActivityCreateSetTheme(this);
setContentView(R.layout.settings);
findViewById(R.id.blackbutton).setOnClickListener(this);
findViewById(R.id.bluebutton).setOnClickListener(this);
findViewById(R.id.pinkbutton).setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.blackbutton:
ThemeUtils.changeToTheme(this, ThemeUtils.BLACK);
break;
case R.id.bluebutton:
ThemeUtils.changeToTheme(this, ThemeUtils.BLUE);
break;
case R.id.pinkbutton:
ThemeUtils.changeToTheme(this, ThemeUtils.PINK);
break;
}
}
}
My Sharedpreference:
public class SharedPreferencesManager {
private SharedPreferences sPreferences;
private SharedPreferences.Editor sEditor;
private Context context;
public SharedPreferencesManager(Context context){
this.context = context;
sPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
private SharedPreferences.Editor getEditor(){
return sPreferences.edit();
}
public void storeBoolean(String tag, boolean value){
sEditor = getEditor();
sEditor.putBoolean(tag,value);
sEditor.commit();
}
public void storeString(String tag, String str){
sEditor = getEditor();
sEditor.putString(tag, str);
sEditor.commit();
}
public boolean retrieveBoolean(String tag, boolean defValue){
return sPreferences.getBoolean(tag,defValue);
}
public String retrieveString(String tag, String defStr){
return sPreferences.getString(tag, defStr);
}
public int retrieveInt(String tag, int defValue){
return sPreferences.getInt(tag, defValue);
}
public void storeInt(String tag, int defValue){
sEditor = getEditor();
sEditor.putInt(tag, defValue);
sEditor.commit();
}
}
This is my ThemeUtils:
public class ThemeUtils
{
private static int cTheme;
public final static int BLACK = 0;
public final static int BLUE = 1;
public final static int PINK = 2;
public static void changeToTheme(Activity activity, int theme)
{
cTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
public static void onActivityCreateSetTheme(Activity activity)
{
switch (cTheme)
{
default:
case BLACK:
activity.setTheme(R.style.BlackTheme);
break;
case BLUE:
activity.setTheme(R.style.BlueTheme);
break;
case PINK:
activity.setTheme(R.style.PinkTheme);
break;
}
}
}
If I close the App and restart them the theme will reset. 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, it's still that new theme.
回答1:
All you need to do is store the Theme ID in SharedPreferences
when a Theme is selected, and retrieve it in onCreate()
when the Activity is loaded.
First, create a method in ThemeUtils
so that you can change cTheme
when the app starts:
private static int cTheme;
public static void setTheme(int t){
cTheme = t;
}
Then, change your Activity code to store the Theme when one is selected, and get the saved Theme on app startup if one had been selected previously:
public class SettingsActivity extends Activity implements OnClickListener {
private SharedPreferencesManager prefs; //added
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
prefs = new SharedPreferencesManager(this); //get SharedPreferencesManager instance
int t = prefs.retrieveInt("theme", 0); //get stored theme, zero is default
ThemeUtils.setTheme(t); //Set the stored theme, will default to Black
ThemeUtils.onActivityCreateSetTheme(this);
setContentView(R.layout.settings);
findViewById(R.id.blackbutton).setOnClickListener(this);
findViewById(R.id.bluebutton).setOnClickListener(this);
findViewById(R.id.pinkbutton).setOnClickListener(this);
}
public void onClick(View v)
{
switch (v.getId())
{
case R.id.blackbutton:
prefs.storeInt("theme", 0); //store black theme
ThemeUtils.changeToTheme(this, ThemeUtils.BLACK);
break;
case R.id.bluebutton:
prefs.storeInt("theme", 1); //store blue theme
ThemeUtils.changeToTheme(this, ThemeUtils.BLUE);
break;
case R.id.pinkbutton:
prefs.storeInt("theme", 2); //store pink theme
ThemeUtils.changeToTheme(this, ThemeUtils.PINK);
break;
}
}
}
来源:https://stackoverflow.com/questions/30088093/how-to-store-theme-on-sharedpreference-in-android