如何在 Android 中的 SharedPreference 上存储主题 [英] How to store theme on SharedPreference in Android

查看:34
本文介绍了如何在 Android 中的 SharedPreference 上存储主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 sharedpreference 来保存我的主题,所以我使用了以下代码:

I'm trying to save my theme by using sharedpreference, so I used this code:

我的设置活动:

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;

    }

    }
}

我的共享偏好:

 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();
}
}

这是我的 ThemeUtils:

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.

推荐答案

您需要做的就是在选择主题时将主题 ID 存储在 SharedPreferences 中,并在 onCreate 中检索它() 当 Activity 被加载时.

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.

首先,在ThemeUtils中创建一个方法,这样你就可以在应用启动时改变cTheme:

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;
        }
    }
}

这篇关于如何在 Android 中的 SharedPreference 上存储主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆