且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用共享preferences保存的Andr​​oid主题

更新时间:2023-02-02 20:17:41

在这里您可以找到有关的共享preferences 的,基本上你应该存储所选主题的数量,当应用程序启动时,检查哪一个存储在共享preferences。检索值你可以经过存储它在全球范围内检查等活动的主题数(这样,你就不必检查共享preferences每次应用程序启动时刚)。

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).

更新0

下面,处理共享preference东西类:

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

}

使用这个类可以存储并从共享preferences检索/ diferent类型的值。在你的情况,你需要存储一个主题的价值。您有:

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

您可以打电话给你的的onClick()处理这个功能,或者你认为这是更好:)

You could call this function on your onClick() handler, or where you think it's better :)

要检索的主题值存储,调用低于code你onCreate方法:

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

请记住,当你想要从共享preferences值,将不得不通过默认值,因此如果没有存储相关的标签的主题,它将返回默认值。

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.

我没有测试此code,也许你得的Adpat这一点。

I didn't test this code, maybe you'll have to adpat it a little.