2012-03-16 57 views
0

我有兩個不同的活動。一個人的觀點包含RadioButton,讓用戶選擇顏色,然後當用戶在Canvas上畫圖時,選項活動中選擇的顏色用於繪製。 下面的代碼的選項Activity在Android中使用SharedPreferences更改Paint中的顏色

  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); 

不幸的是,顏色沒有變化。請幫助!

回答

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); 
+0

謝謝。多一點幫助?現在色彩變化的作品。但每次打開「選項」屏幕時,即使當前顏色可能是其他顏色,也會選中白色的RadioButton。那麼我該如何改變這一點? – 2012-03-16 11:42:08

+0

@KazekageGaara我編輯了我的答案。 – Luksprog 2012-03-16 12:00:58

+0

謝謝! :-) – 2012-03-16 12:01:59