亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Android Button按鈕點(diǎn)擊背景和文字變化操作

瀏覽:83日期:2022-09-22 18:31:09

Android 原生的按鈕點(diǎn)擊狀態(tài)是有變化的,但是如果是自己加了一個(gè).png格式的圖片為背景色,按鈕點(diǎn)擊就不會有任何效果,為了達(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á)到按鈕文字顏色的變化我們再新建一個(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ǔ)充知識:android studio設(shè)置按鈕和背景融為一體也就是按鈕去除陰影

<Buttonandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:text='@string/button_send'android:onClick='sendMessage' />

以上這篇Android Button按鈕點(diǎn)擊背景和文字變化操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 国产成人精品aaaa视频一区 | 东京不热视频在线观看 | 欧美综合中文字幕久久 | 动漫男女交性动漫网站 | jiucao在线观看精品 | 国模大胆偷拍在线视频 | 亚洲精品国产高清不卡在线 | 成人午夜在线观看国产 | 亚洲国产精品综合久久网络 | 欧美精品一区二区三区在线播放 | 中文字幕在线久热精品 | 亚洲网站免费 | 国产麻豆精品免费密入口 | 欧美一区二区三区免费观看视频 | 香蕉97碰碰视频在线看 | 99久久综合狠狠综合久久男同 | 国产视频网 | www.香蕉视频在线观看 | 国产在线精品一区二区三区 | 男女激情视频国产免费观看 | 爱爱免费看 | 小明看看成人免费 | 欧美日韩 国产区 在线观看 | 妞干网在线视频 | 欧美一级黄色大片 | 1024在线观看免费播放 | 国产初高中生厕所小便 | 国产成人一级片 | 国产精品亚洲专区在线观看 | zsvdy午夜 | 亚洲草逼视频 | 亚洲国产成人综合精品2020 | 男女强吻摸下面揉免费 | 伊人激情久久综合中文字幕 | 国产人碰人摸人爱免费视频 | 欧美亚洲精品在线 | 精品国产三级a∨在线观看 精品国产三级在线观看 | 国产精品久久精品 | 日韩一级欧美一级毛片在线 | 中文字幕在线第一页 | 国产精品人人视频 |