Android:使用SharedPreferences更改特定活动的背景颜色

时间:2014-10-13 16:38:49

标签: android sharedpreferences radio-group

我正在尝试更改活动的背景颜色,用户可以使用Android应用中的3个单选按钮更改颜色。我这样做是使用sharedPreferences。

所以我有活动页面,其中颜色与无线电组一起选择,并且使用sharedPrefernces的代码看起来像这样(我知道switch语句有效,以及当颜色应该改变时我有一个Toast消息apperring) :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preferences);
    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup group, int checkedId) {
            SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();

            String colourSelected = "";
            switch (checkedId) {
                case R.id.radioButton1 : 
                  colourSelected = "YELLOW";
                  editor.putString("colour", colourSelected);
                  editor.commit();
                  break;
                case R.id.radioButton2 : 
                  colourSelected = "YELLOW";
                  editor.putString("colour", colourSelected);
                  editor.commit();
                  break;

                case R.id.radioButton3 : 
                  colourSelected = "BLUE";
                  editor.putString("colour", colourSelected);
                  editor.commit();
                  break;
            }   
        }
    });
}

XML看起来像这样

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.practical3_10327751_donnacha_holmes.Preferences" >

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/radiogroup"
    >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Green" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Yellow" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue" />

</RadioGroup>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back" />

然后有一个活动会改变背景颜色,在尝试更改颜色时看起来像这样:

public class Activity2 extends ActionBarActivity {

RelativeLayout rl;
String colour;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity2);

    SharedPreferences prefs = getSharedPreferences("bgColour", MODE_PRIVATE);
    colour = prefs.getString("Colour", "WHITE");

    rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
    if(colour=="GREEN"){
    rl.setBackgroundColor(Color.GREEN);
    }
    else if(colour=="YELLOW"){
        rl.setBackgroundColor(Color.YELLOW);
    }
    else if(colour=="BLUE"){
        rl.setBackgroundColor(Color.BLUE);
    }
    else{
        rl.setBackgroundColor(Color.RED);
    } 

我知道背景颜色正在改变,因为每次进入此页面时都会将其设置为红色。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

==运算符检查两个字符串是否完全相同。

.equals()方法将检查两个字符串是否具有相同的值。

因此比较字符串使用.equals(),即。将您的比较重写为

 if(colour.equals("GREEN")){
    rl.setBackgroundColor(Color.GREEN);
    }
    else if(colour.equals("YELLOW")){
        rl.setBackgroundColor(Color.YELLOW);
    }
    else if(colour.equals("BLUE")){
        rl.setBackgroundColor(Color.BLUE);
    }
    else{
        rl.setBackgroundColor(Color.RED);
    } 

并且您将值保存为颜色并尝试检索为颜色,因此请更改

 colour = prefs.getString("Colour", "WHITE");

 colour = prefs.getString("colour", "WHITE");