0

2 つの文字列をもたらす意図があります。1 はメッセージの文字列で、もう 1 つは色の文字列です。

以下のコードは、2 つの文字列が受信されることを示しています。「メッセージ」文字列は、正しく機能するテキストビューに表示されます。ただし、テキストビューの色を設定するには、文字列「messagecolor」が必要です。

青、緑、赤の色が定義されている colors.xml ファイルがあります。

したがって、文字列「messagecolor」が青色の場合、テキストは青色になります。赤と緑も同じ。文字列 "messagecolor が blue、red、green のいずれでもない場合、テキストは黒く表示されます。

誰かが私がこれを解決するのを手伝ってくれたら、それは最も高く評価されます.

ありがとうございました

// Get the message from the intent
    Bundle bundle = getIntent().getExtras();
    String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
    String message = bundle.getString(MainActivity.EXTRA_MESSAGE);


    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextColor(getResources().getColor(R.color.blue));
    textView.setTextSize(100);
    textView.setText(message);
4

3 に答える 3

3

どの色がどの文字列で表されているかを突き合わせる代わりに、リソース ID 自体を渡さないのはなぜですか?

于 2013-02-15T15:29:18.160 に答える
0

試す

if (message.equals("messageBlue")) {
    textView.setTextColor(getResources().getColor(R.color.blue));
} else if (message.equals("messageGreen")) {
    textView.setTextColor(getResources().getColor(R.color.green));
} else if (message.equals("messageRed")) {
    textView.setTextColor(getResources().getColor(R.color.red));
} else //default
    textView.setTextColor(getResources().getColor(R.color.black));
于 2013-02-15T15:33:50.423 に答える