改变背景颜色与Android的共享preference [英] Changing background colour with SharedPreference in Android

查看:124
本文介绍了改变背景颜色与Android的共享preference的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要改变我的应用程序的背景颜色与按钮。它应该,两种颜色之间切换,为了这个,我使用的共享preference,但>我还不知道如何存储布尔切换..
我得到这个:

 公共无效方法1(查看视图){    共享preferences设置= getShared preferences(preFS,0);
    共享preferences.Editor编辑= settings.edit();
    editor.putBoolean(作案,作案!);
    editor.commit();
    如果(settings.getBoolean(作案,FALSE)){
        INT I = Color.GREEN;
        的LinearLayout布局=(的LinearLayout)findViewById(R.id.mylayout);
        layout.setBackgroundColor(ⅰ);
    }其他{
        INT J = Color.BLUE;
        的LinearLayout布局=(的LinearLayout)findViewById(R.id.mylayout);
        layout.setBackgroundColor(J);
    }
}


解决方案

要保存,并从$ P $布尔PFS可以使用这样的:

 公共类设置
{私有静态最后弦乐preFS_NAME =com.yourpackage.Settings;
私有静态最后弦乐MODUS =Settings.modus;。私有静态最后一个共享preferences preFS = App.getContext()getShared preferences(preFS_NAME,Context.MODE_PRIVATE);私人设置()
{}公共静态无效setUseGreen(布尔useGreen)
{
    编辑编辑= prefs.edit();    edit.putBoolean(MODUS,useGreen);
    edit.commit();
}公共静态布尔useGreen()
{
    返回prefs.getBoolean(MODUS,FALSE);
}
}

然后在你的活动只是用:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.your_layout);    initModus();
}公共无效initModus()
{
    复选框作案=(复选框)findViewById(R.id.yourChackBoxId);
    modus.setOnCheckedChangeListener(新CompoundButton.OnCheckedChangeListener(){
        @覆盖
        公共无效onCheckedChanged(CompoundButton compoundButton,布尔检查)
        {
            Settings.setUseGreen(选中);
            changeColor(选中);
        }
    });    布尔useGreen = Settings.useGreen();
    modus.setChecked(useGreen);
}
私人无效changeColor(布尔选中)
{
    的LinearLayout布局=(的LinearLayout)findViewById(R.id.mylayout);    如果(useGreen){
        INT绿色= Color.GREEN;
        layout.setBackgroundColor(绿色);
    }其他{
        INT蓝色= Color.BLUE;
        layout.setBackgroundColor(蓝色);
    }
}

I want to change the background color of my app with a button. It should switch between two colors, for this I used SharedPreference, but >I don't know yet how to store the boolean for switching.. I got this:

public void method1(View view) {

    SharedPreferences settings = getSharedPreferences(PREFS, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("modus", !modus);
    editor.commit();
    if (settings.getBoolean("modus", false)) {
        int i = Color.GREEN;
        LinearLayout layout = (LinearLayout) findViewById(R.id.mylayout);
        layout.setBackgroundColor(i);
    } else {
        int j = Color.BLUE;
        LinearLayout layout = (LinearLayout) findViewById(R.id.mylayout);
        layout.setBackgroundColor(j);
    }
}

解决方案

To save and get boolean from prefs you can use this :

public class Settings
{

private static final String PREFS_NAME = "com.yourpackage.Settings";
private static final String MODUS = "Settings.modus";

private static final SharedPreferences prefs = App.getContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);

private Settings()
{

}

public static void setUseGreen(boolean useGreen)
{
    Editor edit = prefs.edit();

    edit.putBoolean(MODUS, useGreen);


    edit.commit();
}

public static boolean useGreen()
{
    return prefs.getBoolean(MODUS, false);
}
}

And then in your Activity just use this :

    @Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.your_layout);

    initModus();
}

public void initModus()
{
    CheckBox modus = (CheckBox)findViewById(R.id.yourChackBoxId);
    modus.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean checked)
        {
            Settings.setUseGreen(checked);
            changeColor(checked);
        }
    });

    boolean useGreen = Settings.useGreen();
    modus.setChecked(useGreen);
}


private void changeColor(boolean checked)
{
    LinearLayout layout = (LinearLayout) findViewById(R.id.mylayout);

    if (useGreen) {
        int green = Color.GREEN;
        layout.setBackgroundColor(green);
    } else {
        int blue = Color.BLUE;
        layout.setBackgroundColor(blue);
    }
}

这篇关于改变背景颜色与Android的共享preference的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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