使用共享首选项保存android主题
您好,我正在尝试保存具有共享首选项的主题。当用户点击带有某个主题的按钮时,我希望该主题设置为默认并保存,这样当他们重新打开应用程序时,它仍然是新主题 主要活动:使用共享首选项保存android主题,android,themes,sharedpreferences,Android,Themes,Sharedpreferences,您好,我正在尝试保存具有共享首选项的主题。当用户点击带有某个主题的按钮时,我希望该主题设置为默认并保存,这样当他们重新打开应用程序时,它仍然是新主题 主要活动: /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
/** 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;
}
}
}
UTIL:
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;
我还希望为所有活动保存主题,而不仅仅是单击按钮的活动。谢谢我提供了一个应该适合您的示例。有关更多信息,请参阅。由于PREFS_名称是公共的,所以可以调用GetSharedReferences(MyActivity.PREFS_名称,0);从其他活动访问相同的SharedReference
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//"theme" is the key sTheme is the value you're saving
editor.putInt("theme", sTheme);
editor.commit();
在你们班上
public static final String PREFS_NAME = "MyPrefsFile";
将此代码放在SharedReferences中要保存主题选择的位置
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//"theme" is the key sTheme is the value you're saving
editor.putInt("theme", sTheme);
editor.commit();
把这段代码放在你想拿回那个值的地方
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//"theme" is the same key 0 is the default value
int theme = settings.getInt("theme", 0);
我已经包括了一个对你有用的例子。有关更多信息,请参阅。由于PREFS_名称是公共的,所以可以调用GetSharedReferences(MyActivity.PREFS_名称,0);从其他活动访问相同的SharedReference
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//"theme" is the key sTheme is the value you're saving
editor.putInt("theme", sTheme);
editor.commit();
在你们班上
public static final String PREFS_NAME = "MyPrefsFile";
将此代码放在SharedReferences中要保存主题选择的位置
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//"theme" is the key sTheme is the value you're saving
editor.putInt("theme", sTheme);
editor.commit();
把这段代码放在你想拿回那个值的地方
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//"theme" is the same key 0 is the default value
int theme = settings.getInt("theme", 0);
在这里你可以找到一个教程,基本上你应该存储所选主题的编号,当应用程序启动时,检查哪个主题存储在SharedReferences上。在检索到该值后,您可以全局“存储”该值,以检查其他活动的主题编号(这样,您就不必每次都检查SharedReferences,就在应用程序启动时) 更新0 下面是一个处理SharedReference内容的类:
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();
}
//Incorrect Bracket Closing Removal.
使用此类,您可以在SharedReferences上存储和检索不同类型的值。在您的情况下,需要存储主题的值。你有:
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
因此,在用户选择主题后,您应该调用:
new SharedPreferencesManager(getApplicationContext()).storeInt("theme", THEME_WHITE);
您可以在onClick()
处理程序上调用此函数,或者在您认为更好的地方调用:)
要检索存储的主题值,请在onCreate方法上调用以下代码:
Utils.changeToTheme(this, new SharedPreferencesManager(this).retrieveInt("theme", THEME_WHITE));
请记住,当您要从SharedReferences检索值时,必须传递默认值,因此如果未存储与标记“theme”相关的任何内容,它将返回默认值
我没有测试这段代码,也许你需要修改一下。在这里你可以找到一个教程,基本上你应该存储所选主题的编号,当应用程序启动时,检查哪个主题存储在SharedReferences上。在检索到该值后,您可以全局“存储”该值,以检查其他活动的主题编号(这样,您就不必每次都检查SharedReferences,就在应用程序启动时) 更新0 下面是一个处理SharedReference内容的类:
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();
}
//Incorrect Bracket Closing Removal.
使用此类,您可以在SharedReferences上存储和检索不同类型的值。在您的情况下,需要存储主题的值。你有:
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
因此,在用户选择主题后,您应该调用:
new SharedPreferencesManager(getApplicationContext()).storeInt("theme", THEME_WHITE);
您可以在onClick()
处理程序上调用此函数,或者在您认为更好的地方调用:)
要检索存储的主题值,请在onCreate方法上调用以下代码:
Utils.changeToTheme(this, new SharedPreferencesManager(this).retrieveInt("theme", THEME_WHITE));
请记住,当您要从SharedReferences检索值时,必须传递默认值,因此如果未存储与标记“theme”相关的任何内容,它将返回默认值
我没有测试这段代码,也许您需要调整一下。在您的主要活动中,对onClick进行以下更改:
@Override
public void onClick(View v)
{
//Setup the SharedPreferences
SharedPreferences mSettings = this.getSharedPreferences("Themes", 0);
//SharedPrerefences Editor (to add content to SP)
SharedPreferences.Editor editor = mSettings.edit();
switch (v.getId())
{
case R.id.button1:
Utils.changeToTheme(this, Utils.THEME_DEFAULT);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
case R.id.button2:
Utils.changeToTheme(this, Utils.THEME_WHITE);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
case R.id.button3:
Utils.changeToTheme(this, Utils.THEME_BLUE);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
}
}
然后,在onCreate的顶部,在super和setContentView之前添加以下内容:
SharedPreferences mSettings = this.getSharedPreferences("Themes", 0);
Utils.SetTheme(mSettings.getInt("theme", 0));
我们在这里所做的是:
现在,当您单击白色主题按钮时,它将应用它。在您的主要活动中,对onClick进行以下更改:
@Override
public void onClick(View v)
{
//Setup the SharedPreferences
SharedPreferences mSettings = this.getSharedPreferences("Themes", 0);
//SharedPrerefences Editor (to add content to SP)
SharedPreferences.Editor editor = mSettings.edit();
switch (v.getId())
{
case R.id.button1:
Utils.changeToTheme(this, Utils.THEME_DEFAULT);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
case R.id.button2:
Utils.changeToTheme(this, Utils.THEME_WHITE);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
case R.id.button3:
Utils.changeToTheme(this, Utils.THEME_BLUE);
//Add the theme's int to SharedPreferences
editor.putInt("theme", Utils.THEME_TEAL);
//Apply the save
editor.apply();
break;
}
}
然后,在onCreate的顶部,在super和setContentView之前添加以下内容:
SharedPreferences mSettings = this.getSharedPreferences("Themes", 0);
Utils.SetTheme(mSettings.getInt("theme", 0));
我们在这里所做的是:
现在,当您单击“白色主题”按钮时,它将应用它。我认为我的设置不正确:(您能否在我的代码顶部显示此示例。例如,假设我正在尝试保存主题\u白色主题谢谢我认为我的设置不正确:(你能把这个例子显示在我的代码中吗?)例如,我想我正在试图保存主题\u白色主题谢谢我认为我设置得不对:(你能把这个例子显示在我的代码中吗?)例如,我正在试图保存主题\u白色主题谢谢我认为这是这个任务最简单的解决方案我认为我设置得不对:(你能把这个例子展示在我的代码中吗?比如说,我正在尝试保存主题\白色主题谢谢,我认为这是这个任务最简单的解决方案