Android Button按鈕點(diǎn)擊背景和文字變化操作
Android 原生的按鈕點(diǎn)擊狀態(tài)是有變化的,但是如果是自己加了一個(gè).png格式的圖片為背景色,按鈕點(diǎn)擊就不會(huì)有任何效果,為了達(dá)到點(diǎn)擊按鈕有一閃的效果,我們就需要準(zhǔn)備兩張圖進(jìn)行切換, 而且文字也要變色,老規(guī)矩廢話不多說直接上代碼:
按鈕背景圖片放在 drawable/background_button.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/bg_press" android:state_pressed="true"/> <item android:drawable="@drawable/bg_normal" android:state_enabled="true"/> <item android:drawable="@drawable/bg_normal"/> </selector>
準(zhǔn)備兩張圖片一張為bg_press.png, 一張為 bg_normal.png。
在需要變化的按鈕中設(shè)置:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="50dp" android:focusable="false" android:gravity="center" android:textSize="24px" android:text="@string/str_tethering_modify" android:background="@drawable/background_button" />
這有背景色變化就解決完了,下面到按鈕上的文字了,現(xiàn)在點(diǎn)擊按鈕按鈕上的文字是沒有變化的,為了達(dá)到按鈕文字顏色的變化我們?cè)傩陆ㄒ粋€(gè)xml文件。
按鈕顏色變化 drawable/button_color.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#975508"/> <item android:state_focused="false" android:state_pressed="false" android:color="#E5960E"/> <item android:state_focused="true" android:color="#975508"/> <item android:state_focused="false" android:color="#E5960E"/> </selector>
加入到我們的按鈕textColor中
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="50dp" android:focusable="false" android:gravity="center" android:textSize="24px" android:textColor="@drawable/button_color" android:text="@string/str_tethering_modify" android:background="@drawable/background_button" />
這樣直接使用背景和文字就都有點(diǎn)擊效果啦,但是如果有這樣一個(gè)需求,在某些條件下需要再設(shè)置按鈕文字的顏色button.setTextColor(color),這樣設(shè)置完后,發(fā)現(xiàn)我們按鈕上文字點(diǎn)擊又沒有變化了,我之前試著直接 button.setTextColor(R.drawable.button_color);發(fā)現(xiàn)這樣是沒有任何用處的。這樣就需要使用 ColorStateList 來解決,顧名思義,就是定義顏色的狀態(tài)列表,通過監(jiān)聽按鈕不同狀態(tài)來設(shè)置不同的顏色,
老規(guī)矩,廢話不多說了,直接貼代碼:
/** * 按鈕點(diǎn)擊顏色變化 */ private ColorStateList colorStateList; colorStateList = (ColorStateList)getResources().getColorStateList(R.drawable.button_color); if(xxx){ button.setTextColor(Color.White); }else{ button.setTextColor(colorStateList); }
這樣就完美解決了按鈕點(diǎn)擊狀態(tài)的變化啦。
補(bǔ)充知識(shí):android studio設(shè)置按鈕和背景融為一體也就是按鈕去除陰影
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" style="?android:attr/borderlessButtonStyle" />
以上這篇Android Button按鈕點(diǎn)擊背景和文字變化操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)述Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法
本篇文章主要對(duì)Android中實(shí)現(xiàn)APP文本內(nèi)容的分享發(fā)送與接收方法進(jìn)行介紹,相信對(duì)大家學(xué)習(xí)會(huì)有很好的幫助,需要的朋友一起來看下吧2016-12-12Android顯示系統(tǒng)SurfaceFlinger分析
本文詳細(xì)講解了Android顯示系統(tǒng)SurfaceFlinger,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12Android 自動(dòng)判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用
本篇文章小編為大家介紹,在Android中 自動(dòng)判斷是電話,網(wǎng)址,EMAIL方法之Linkify的使用。需要的朋友參考下2013-04-04Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符
Kotlin 是一種在 Java 虛擬機(jī)上運(yùn)行的靜態(tài)類型編程語(yǔ)言,被稱之為 Android 世界的Swift,由 JetBrains 設(shè)計(jì)開發(fā)并開源,下面這篇文章主要給大家介紹了關(guān)于Kotlin新手基礎(chǔ)學(xué)習(xí)之Elvis操作符的相關(guān)資料,需要的朋友可以參考下。2017-12-12Android中正確使用字體圖標(biāo)(iconfont)的方法
IconFont字體不僅僅流行于Web開發(fā),在移動(dòng)開發(fā)中也漸漸的使用的范圍更廣泛。這篇文章主要介紹了在Android開發(fā)中使用icon font的代碼和方法。對(duì)大家學(xué)習(xí)使用iconfont有一定的參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2016-10-10詳解Android中用于線程處理的AsyncTask類的用法及源碼
這篇文章主要介紹了Android中用于線程處理的AsyncTask類的用法及源碼,講到了實(shí)現(xiàn)AsyncTask中所用到的Handler及線程池等要點(diǎn),需要的朋友可以參考下2016-05-05Android自定義View實(shí)現(xiàn)星星評(píng)分效果
這篇文章主要為大家詳細(xì)介紹了Android如何利用自定義View實(shí)現(xiàn)一個(gè)星星評(píng)分的控件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-11-11Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題分析
這篇文章主要介紹了Android開發(fā)中計(jì)算器的sin、cos及tan值計(jì)算問題,結(jié)合實(shí)例形式分析了Android三角函數(shù)運(yùn)算中的弧度與角度計(jì)算問題與相關(guān)解決方法,需要的朋友可以參考下2017-11-11