私はRadioButton
を使用して色を選択するオプションActivity
を持っています。既定では、の色はandroid:checked="true"
に設定されています。今度私がCanvas
に到達したら、RadioButton
が選択されたことに応じて、Paint
の色を動的に変更する必要があります。 Canvas
のためのクラスでPaintオブジェクトの色を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
: は、ここで私が試してみましたコードだ
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);
しかし、私はCanvas
ためActivity
が開始されるたびにNullPointerException
を取得します。デフォルトでは、白が色でなければならないことに注意することが重要です。また、これはそれを永続的に格納しませんか?これについてSharedPreferencesを調べる必要がありますか?
2つ目のアクティビティを開始する場所ではない場合は、2番目のアクティビティを開始する際に関連コードを入力することができます。 – Deva
私はこのアクティビティから他のアクティビティを開始しません。私はこのアプローチでは全く間違っている、つまりputExtra()を選択していると感じています。 –