Android選中字體顏色改變

一種是在layout中使用textview,然後再代碼中設置字體顏色改變,我這裏寫了個工具類,以供參考。

public class TextcolorUtil {
    public static void setTextColor(Context context, int index, TextView... textViews) {
        for (int i = 0; i < textViews.length; i++) {
            if (i == index) {
                textViews[i].setTextColor(context.getResources().getColor(R.color.main_buttomtext_yellow));
            } else {
                textViews[i].setTextColor(context.getResources().getColor(R.color.main_buttomtext_grey));
            }
        }
    }
}

使用時如下:

TextcolorUtil.setTextColor(getActivity(),3,tv1,tv2,tv3,tv4);
還有一種更簡單的方式,直接在layout中使用單選框radiobutton,設置textcolor選擇器
 <RadioButton
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:textSize="10sp"
    android:text="@string/booklist"
    android:button="@null"
    android:textColor="@drawable/selector_text_color" />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:color="@color/main_buttomtext_yellow" android:state_checked="true"/>
    <item android:color="@color/main_buttomtext_grey" android:state_checked="false" />

</selector>
推薦第二種方式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章