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>
注意CheckWhite
有android: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);
不幸的是,顏色沒有變化。請幫助!
謝謝。多一點幫助?現在色彩變化的作品。但每次打開「選項」屏幕時,即使當前顏色可能是其他顏色,也會選中白色的RadioButton。那麼我該如何改變這一點? – 2012-03-16 11:42:08
@KazekageGaara我編輯了我的答案。 – Luksprog 2012-03-16 12:00:58
謝謝! :-) – 2012-03-16 12:01:59