0
我有一个选项Activity
,其中我使用RadioButton
选择颜色。默认情况下,White
颜色设置为android:checked="true"
。现在,当我到达我的Canvas
时,我需要动态更改Paint
颜色,具体取决于哪个RadioButton
被选中。 下面是我曾尝试代码:在Android中动态更改绘画对象的颜色
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;
}
Intent i = new Intent(HandwritingRecognitionOptionTab.this,HandwritingRecognitionCanvas.class);
i.putExtra("setColor",radioButtonSelected);
//startActivity(i); // because I don't want to start the activity immediately after this
在类Canvas
:
Bundle getValue = getIntent().getExtras();
drawColor = getValue.getString("setColor");
if(drawColor.equals("White"))
intColor = 1;
if(drawColor.equals("Red"))
intColor = 2;
if(drawColor.equals("Blue"))
intColor = 3;
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.WHITE);
if(intColor == 1)
mPaint.setColor(Color.WHITE);
if(intColor == 2)
mPaint.setColor(Color.RED);
if(intColor == 3)
mPaint.setColor(Color.BLUE);
,但我得到每当Activity
为Canvas
启动一个NullPointerException
。重要的是要注意,默认情况下,白色应该是颜色。另外,这不会永久保存它吗?我应该看看SharedPreferences吗?
如果这不是你开始第二个活动的地方,那么你可以把无论你是否开始第二个活动的相关代码。 – Deva 2012-03-16 09:36:09
我不从此活动开始其他活动。我感觉我在这种方法中完全错误,即选择putExtra()。 – 2012-03-16 09:38:36