使用共享首选项保存主题

时间:2015-05-06 21:40:05

标签: java android sharedpreferences

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

我的设置活动:

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:

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;

}

}

}

如果我关闭应用程序并重新启动它们,主题将重置。当用户点击具有特定主题的按钮时,我希望将该主题设置为默认并保存,以便当他们重新打开应用程序时,它仍然是新主题。

1 个答案:

答案 0 :(得分:0)

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

首先,在ThemeUtils中创建一个方法,以便在应用启动时更改cTheme

private static int cTheme;

public static void setTheme(int t){
    cTheme = t;
}

然后,更改您的活动代码以在选择一个时存储主题,并在应用启动时获取已保存的主题(如果之前已选择):

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