簡體   English   中英

更改偵聽器中的按鈕文本顏色

[英]Change button text color in listener

我正在編寫簡單的應用程序,並且在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>

您可以嘗試使用此代碼進行顏色選擇。

更改:

mButton3.setTextColor(R.color.white);

至:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM