Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法
本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)按鈕點(diǎn)擊切換背景并修改文字顏色的方法。分享給大家供大家參考,具體如下:
其實(shí)原理很簡單,用到的是selector,用來設(shè)置android:background和android:textcolor屬性,selector可以用來設(shè)置默認(rèn)時候、點(diǎn)擊時候的背景圖片和文字顏色的屬性,過程如下:
這兩個文件如下:
1.當(dāng)點(diǎn)擊按鈕,改變文字的顏色:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_enabled="true" android:state_pressed="false"
android:color="#000000" />
<item android:state_enabled="false" android:color="#000000" />
<item android:state_pressed="true" android:color="#ffffff" />
<item android:state_focused="true" android:color="#000000" />
</selector>
2.當(dāng)點(diǎn)擊按鈕,改變按鈕的背景:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:Android="http://schemas.android.com/apk/res/android"> <!-- 定義按鈕按下時的圖片 --> <item Android:drawable="@drawable/blue" Android:state_pressed="true"/> <!-- 定義按鈕默認(rèn)的圖片 --> <item Android:drawable="@drawable/gray"/> </selector>
是不是很簡單,不用再用java代碼來進(jìn)行設(shè)置,只需要設(shè)置到你所需要的Textview或者button,如下使用方式:
<TextView android:id="@+id/menu_logout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="@drawable/btnselector" android:drawableLeft="@drawable/menu_logout" android:drawablePadding="10dp" android:gravity="center_vertical" android:text="@string/logout" android:textColor="@drawable/btnselectorcolor" android:textSize="@dimen/Menu_Font_Size" />
成功完成!?。?!
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android自定義控件之水平圓點(diǎn)加載進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android自定義控件之水平圓點(diǎn)加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
Android XML設(shè)置屏幕方向(android:screenOrientation)詳解
這篇文章主要介紹了Android XML設(shè)置屏幕方向(android:screenOrientation)詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android 中RecycleView實(shí)現(xiàn)item的點(diǎn)擊事件
這篇文章主要介紹了Android 中RecycleView實(shí)現(xiàn)item的點(diǎn)擊事件的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Server和Client獲得Service Manager接
本文主要介紹Android IPC通信Binder中的Server和Client獲得Service Manager接口,這里詳細(xì)的說明了如何實(shí)現(xiàn)Service Manager接口,對研究Android源碼的朋友提供幫助,有需要的小伙伴可以參考下2016-08-08
Android開發(fā)微信APP支付功能的要點(diǎn)小結(jié)
微信支付現(xiàn)在在日常生活中隨處可見,而關(guān)于Android開發(fā)微信支付的文章網(wǎng)上也很多,所以這篇文章主要介紹的是在Android開發(fā)微信APP支付功能的要注意的要點(diǎn),有需要的可以參考借鑒。2016-08-08
Android如何實(shí)現(xiàn)動態(tài)滾動波形圖(心電圖)功能
這篇文章主要介紹了Android如何實(shí)現(xiàn)動態(tài)滾動波形圖(心電圖)功能,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03

