Android Text Click Color

當點擊Button時,想要改變Button上文字的顏色,我們可使用resource xml的方式來實現。

步驟如下

Step 1: 建立color資料夾

在res底下建立color資料夾。

Step 2: 建立color檔案

在color資料夾底下建立xml檔案, 內容如下

text_color.xml
1
2
3
4
5
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#ffffffff"/>
<item android:state_focused="true" android:color="#ffffffff"/>
<item android:color="#d0b9ff"/>
</selector>

Step 3: 設定文字顏色

main_layout.xml
1
2
3
4
5
6
7
8
<Button
android:id="@+id/bt_adv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/adv_settings"
android:textColor="@color/text_color"
/>
作者

Nick Lin

發表於

2019-10-25

更新於

2023-01-18

許可協議


評論