Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
前言
CompoundButton在XML文件中主要使用下面兩個(gè)屬性。
- checked:指定按鈕的勾選狀態(tài),true表示勾選,false則表示未勾選,默認(rèn)為未勾選。
- button:指定左側(cè)勾選圖標(biāo)的圖形資源,如果不指定就使用系統(tǒng)的默認(rèn)圖標(biāo)。
CompoundButton在java代碼中主要使用下列4種方法。
- setChecked:設(shè)置按鈕的勾選狀態(tài)。
- setButtonDrawable:設(shè)置左側(cè)勾選圖標(biāo)的圖形資源。
- setOnCheckedChangeListener:設(shè)置勾選狀態(tài)變化的監(jiān)聽器。
- isChecked:判斷按鈕是否勾選。
一、復(fù)選框CheckBox
<CheckBox android:id="@+id/ck_system" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="系統(tǒng)CheckBox"/>
二、開關(guān)按鈕Switch
Switch是開關(guān)按鈕,它在選中與取消選中時(shí)可展現(xiàn)的界面元素比復(fù)選框豐富。
Switch控件新添加的XML屬性說(shuō)明如下:
- textOn:設(shè)置右側(cè)開啟時(shí)的文本。
- textOff:設(shè)置左側(cè)關(guān)閉時(shí)的文本。
- track:設(shè)置開關(guān)軌道的背景。
- thumb:設(shè)置開關(guān)標(biāo)識(shí)的圖標(biāo)。
<Switch android:id="@+id/sw_status" android:layout_width="80dp" android:layout_height="30dp" android:padding="5dp"/>
三、單選按鈕RadioButton
單選按鈕要在一組按鈕種選擇其中一項(xiàng),并且不能多選,這要求有個(gè)容器確定這組按鈕的范圍,這個(gè)容器便是單選組RadioGroup。
RadioGroup實(shí)際上是個(gè)布局,同一組RadioButton都要放在同一個(gè)RadioGroup節(jié)點(diǎn)下,除了RadioButton,也允許放置其他控件。
單選組的用法
判斷選中了哪個(gè)單選按鈕,通常不是監(jiān)聽某個(gè)單選按鈕,而是監(jiān)聽單選組的選中事件。
RadioGroup常用的3個(gè)方法:
- check:選中指定資源編號(hào)的單選按鈕。
- getCheckedRadioButtonId:獲取選中狀態(tài)單選按鈕的資源編號(hào)。
- setOnCheckedChangeListener:設(shè)置單選按鈕勾選變化的監(jiān)聽器。
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="請(qǐng)選擇性別"/> <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="男"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="女"/> </RadioGroup>
到此這篇關(guān)于Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解的文章就介紹到這了,更多相關(guān)Android按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android CheckBox中設(shè)置padding無(wú)效解決辦法
- Android開發(fā)之CheckBox的簡(jiǎn)單使用與監(jiān)聽功能示例
- Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼
- Android 中CheckBox的isChecked的使用實(shí)例詳解
- Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件
- Android開關(guān)控件Switch的使用案例
- Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解
- Android UI控件Switch的使用方法
- Android單選按鈕RadioButton的使用方法
相關(guān)文章
Android使用WebView實(shí)現(xiàn)文件下載功能
這篇文章主要為大家詳細(xì)介紹了Android使用WebView實(shí)現(xiàn)文件下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法總結(jié)
這篇文章主要介紹了Android編程計(jì)算函數(shù)時(shí)間戳的相關(guān)方法,結(jié)合實(shí)例形式總結(jié)分析了Android Java、Native、Kernel時(shí)間戳計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2017-05-05Android不規(guī)則封閉區(qū)域填充色彩的實(shí)例代碼
這篇文章主要介紹了Android不規(guī)則封閉區(qū)域填充色彩的實(shí)例代碼, 具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android ListView UI組件使用說(shuō)明
這篇文章主要介紹了Android ListView UI組件使用說(shuō)明,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04android kotlin集成WorkManager實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)的步驟
在Android中使用Kotlin集成WorkManager來(lái)實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)是一個(gè)很常見的需求,下面給大家分享android kotlin集成WorkManager實(shí)現(xiàn)定時(shí)獲取數(shù)據(jù)的步驟,感興趣的朋友跟隨小編一起看看吧2024-08-08