使用SharedPreferences更改android中Paint的颜色

时间:2012-03-16 11:02:57

标签: android

我有两个不同的活动。一个人的视图包含允许用户选择颜色的RadioButton,然后当用户绘制Canvas时,选项活动中选择的颜色用于绘制。 以下是选项Activity的代码:

            RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
            int checkedRadioButton = radioGroup.getCheckedRadioButtonId();

            String radioButtonSelected = "";
            switch (checkedRadioButton) {
              case R.id.CheckRed : radioButtonSelected = "Red";
                                              break;
              case R.id.CheckBlue : radioButtonSelected = "Blue";
                                          break;
              case R.id.CheckWhite : radioButtonSelected = "White";
                                          break;
            }
            SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
            Editor prefsEditor = appSharedPrefs.edit();
            prefsEditor.putString("setColor", radioButtonSelected);
            prefsEditor.commit();

以下是此Activity的xml:

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


    <RadioButton 
        android:id="@+id/CheckWhite"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="White" />

    <RadioButton 
        android:id="@+id/CheckRed" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Red" />

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

</RadioGroup>

请注意CheckWhiteandroid:checked="true"。对于用户以后选择的颜色,我希望这是真的。

以下是执行绘图的Activity的代码:

        SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
        drawColor = appSharedPrefs.getString("setColor", "White");
        if(drawColor.equals("White"))
            intColor = 1;
        if(drawColor.equals("Red"))
            intColor = 2;
        if(drawColor.equals("Blue"))
            intColor = 3;


        mPaint = new Paint();
        if(intColor == 1)
            mPaint.setColor(Color.WHITE);
        if(intColor == 2)
            mPaint.setColor(Color.RED);
        if(intColor == 3)
            mPaint.setColor(Color.BLUE);

不幸的是,颜色不会改变。请帮助!

1 个答案:

答案 0 :(得分:2)

OnCheckedChangedListener添加RadioGroup,在方法onCheckedChanged(RadioGroup group, int checkedId)中添加设置用户颜色首选项的代码。一些代码:

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String radioButtonSelected = "";
                switch (checkedId) {
                  case R.id.CheckRed : radioButtonSelected = "Red";
                                                  break;
                  case R.id.CheckBlue : radioButtonSelected = "Blue";
                                              break;
                  case R.id.CheckWhite : radioButtonSelected = "White";
                                              break;
                }
                SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
                Editor prefsEditor = appSharedPrefs.edit();
                prefsEditor.putString("setColor", radioButtonSelected);
                prefsEditor.commit();

            }
        });

修改 从xml布局中删除android:checked="true"(从白色RadioButon),然后在选项屏幕中根据您的偏好设置选中的RadioButton

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
radioGroup.clearCheck();
SharedPreferences appSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
drawColor = appSharedPrefs.getString("setColor", "White");
if(drawColor.equals("White"))
    radioGroup.check(R.id.CheckWhite);
if(drawColor.equals("Red"))
    radioGroup.check(R.id.CheckRed);
if(drawColor.equals("Blue"))
    radioGroup.check(R.id.CheckBlue);