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)時(shí)候、點(diǎn)擊時(shí)候的背景圖片和文字顏色的屬性,過程如下:
這兩個(gè)文件如下:
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"> <!-- 定義按鈕按下時(shí)的圖片 --> <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 ListView優(yōu)化之提高android應(yīng)用效率
android listview優(yōu)化做的好是提高androoid應(yīng)用效率的前提條件,本文給大家介紹Android ListView優(yōu)化之提高android應(yīng)用效率,對android listview優(yōu)化相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2015-12-12Android實(shí)現(xiàn)簡單點(diǎn)贊動畫
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡單點(diǎn)贊動畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)橫屏切換科學(xué)計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06Android自定義view實(shí)現(xiàn)圖片選色器
這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)圖片選色器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android開發(fā)中Google為什么不讓用Handler的runWithScissors()
這篇文章主要介紹了Android開發(fā)中Google為什么不讓用Handler的runWithScissors(),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Android使用TransitionDrawable漸變切換多張圖片
這篇文章主要為大家詳細(xì)介紹了Android使用TransitionDrawable漸變切換多張圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android保持屏幕常亮2種實(shí)現(xiàn)方法
這篇文章主要介紹了Android保持屏幕常亮2種實(shí)現(xiàn)方法,本文分別給出示例代碼,需要的朋友可以參考下2015-05-05Android使用ViewPager實(shí)現(xiàn)啟動引導(dǎo)頁
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)第一次啟動引導(dǎo)頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android中基于XMPP協(xié)議實(shí)現(xiàn)IM聊天程序與多人聊天室
這篇文章主要介紹了Android中基于XMPP協(xié)議實(shí)現(xiàn)IM聊天程序與多人聊天室的方法,XMPP基于XML數(shù)據(jù)格式傳輸,一般用于即時(shí)消息(IM)以及在線現(xiàn)場探測,需要的朋友可以參考下2016-02-02android 自定義圓角button效果的實(shí)例代碼(自定義view Demo)
這篇文章主要介紹了android 自定義圓角button(自定義View Demo),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12