更改侦听器中的按钮文本颜色

时间:2014-09-15 21:07:21

标签: android button onclicklistener

我正在编写简单的应用程序,我在OnClickListener中更改文本颜色时遇到问题。我创建了自己的Listener类:

@Override
public void onClick(View button) {
    switch (mNumber) {
    case 1:
        mButton1.setTextColor(R.color.white);
        mButton1.setBackgroundResource(R.drawable.blue_rounded_rectangle);
        mButton2.setBackgroundResource(R.drawable.gray_rounded_rectangle);
        mButton2.setTextColor(R.color.gray);
        mButton3.setBackgroundResource(R.drawable.gray_rounded_rectangle);
        mButton3.setTextColor(R.color.gray);
        mImage.setImageResource(mLessonData.getFirstImage(mLesson));
        mComment.setText(mLessonData.setFirstImageComment(mLesson));
        break;
    case 2:
        mButton1.setBackgroundResource(R.drawable.gray_rounded_rectangle);
        mButton1.setTextColor(R.color.gray);
        mButton2.setBackgroundResource(R.drawable.blue_rounded_rectangle);
        mButton2.setTextColor(R.color.white);
        mButton3.setBackgroundResource(R.drawable.gray_rounded_rectangle);
        mButton3.setTextColor(R.color.gray);
        mImage.setImageResource(mLessonData.getSecondImage(mLesson));
        mComment.setText(mLessonData.setSecondImageComment(mLesson));
        break;
    case 3:
        mButton1.setBackgroundResource(R.drawable.gray_rounded_rectangle);
        mButton1.setTextColor(R.color.gray);
        mButton2.setBackgroundResource(R.drawable.gray_rounded_rectangle);
        mButton2.setTextColor(R.color.gray);
        mButton3.setBackgroundResource(R.drawable.blue_rounded_rectangle);
        mButton3.setTextColor(R.color.white);
        mImage.setImageResource(mLessonData.getThirdImage(mLesson));
        mComment.setText(mLessonData.setThirdImageComment(mLesson));
        break;
    }

}

背景颜色,更改TextView中的文本和ImageView中的图像工作正常。问题在于按钮中的字体颜色。它总是变成dark_grey。任何人都知道为什么它以这种方式运作?

颜色的定义:

<color name="white">#FFFFFF</color>
<color name="gray">#AAAAAA</color>

1 个答案:

答案 0 :(得分:2)

您可以尝试使用此代码进行颜色选择。

变化:

mButton3.setTextColor(R.color.white);

为:

button.setTextColor(getApplication().getResources().getColor(R.color.white)); //TAKE DEFAULT COLOR