【发布时间】:2015-03-18 01:37:07
【问题描述】:
您好,我正在尝试保存具有共享偏好的主题。当用户单击具有特定主题的按钮时,我希望将该主题设置为默认主题并保存,这样当他们重新打开应用程序时,它仍然是那个新主题。
主要活动:
/** 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;
}
}
}
实用程序:
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;
我还希望为所有活动保存主题,而不仅仅是单击按钮的活动。谢谢
【问题讨论】:
标签: android themes sharedpreferences