Android Button按鈕點擊背景和文字變化操作_第1頁
Android Button按鈕點擊背景和文字變化操作_第2頁
Android Button按鈕點擊背景和文字變化操作_第3頁
Android Button按鈕點擊背景和文字變化操作_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

第AndroidButton按鈕點擊背景和文字變化操作Android原生的按鈕點擊狀態是有變化的,但是如果是自己加了一個.png格式的圖片為背景色,按鈕點擊就不會有任何效果,為了達到點擊按鈕有一閃的效果,我們就需要準備兩張圖進行切換,而且文字也要變色,老規矩廢話不多說直接上代碼:

按鈕背景圖片放在drawable/background_button.xml

xmlversion=1.0encoding=utf-8

selectorxmlns:android=/apk/res/android

itemandroid:drawable=@drawable/bg_pressandroid:state_pressed=true/

itemandroid:drawable=@drawable/bg_normalandroid:state_enabled=true/

itemandroid:drawable=@drawable/bg_normal/

/selector

準備兩張圖片一張為bg_press.png,一張為bg_normal.png。

在需要變化的按鈕中設置:

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/

這有背景色變化就解決完了,下面到按鈕上的文字了,現在點擊按鈕按鈕上的文字是沒有變化的,為了達到按鈕文字顏色的變化我們再新建一個xml文件。

按鈕顏色變化drawable/button_color.xml

xmlversion=1.0encoding=utf-8

selectorxmlns:android=/apk/res/android

itemandroid:state_pressed=trueandroid:color=#975508/

itemandroid:state_focused=falseandroid:state_pressed=falseandroid:color=#E5960E/

itemandroid:state_focused=trueandroid:color=#975508/

itemandroid:state_focused=falseandroid: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/

這樣直接使用背景和文字就都有點擊效果啦,但是如果有這樣一個需求,在某些條件下需要再設置按鈕文字的顏色button.setTextColor(color),這樣設置完后,發現我們按鈕上文字點擊又沒有變化了,我之前試著直接button.setTextColor(R.drawable.button_color);發現這樣是沒有任何用處的。這樣就需要使用ColorStateList來解決,顧名思義,就是定義顏色的狀態列表,通過監聽按鈕不同狀態來設置不同的顏色,

老規矩,廢話不多說了,直接貼代碼:

*按鈕點擊顏色變化

privateColorStateListcolorStateList;

colorStateList=(ColorStateList)getResources().getColorStateList(R.drawable.button_color);

if(xxx){

button.setTextColor(Color.White);

}else{

button.setTextColor(colorStateList);

}

這樣就完美解決了按鈕點擊狀態的變化啦。

補充知識:androidstudio設置按鈕和背景融為一體也就是按鈕去除陰影

Button

android:layout_width=wrap_content

android:layout_height=wrap_con

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論