Android單選按鈕RadioButton的使用方法
單選按鈕要在一組中選擇一項(xiàng),并且不能多選。
同一組RadioButton要放在同一個(gè)RadioGroup節(jié)點(diǎn)下。
RadioButton默認(rèn)未選中,點(diǎn)擊后選中但是再次點(diǎn)擊不會(huì)取消選中。
RadioButton經(jīng)常會(huì)更換按鈕圖標(biāo),如果通過button屬性變更圖標(biāo),那么圖標(biāo)與文字就會(huì)挨得很近。為了拉開圖標(biāo)與文字之間的距離,得換成drawableLeft屬性展示新圖標(biāo)(不要忘記把button改為@null),再設(shè)置drawablePadding即可指定間隔距離。
復(fù)現(xiàn)代碼時(shí)出現(xiàn)了一個(gè)錯(cuò)誤,處理單選按鈕的響應(yīng),要先寫一個(gè)單選監(jiān)聽器實(shí)現(xiàn)接口 RadioGroup.OnCheckedChangeListener,而不是復(fù)合按鈕的CompoundButton.OnCheckedChangeListener。
MainActivity
package com.example.middle; import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.RadioGroup.OnCheckedChangeListener; public class RadioVerticalActivity extends AppCompatActivity implements OnCheckedChangeListener { private TextView tv_marry; // 聲明一個(gè)文本視圖對(duì)象 @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio_vertical);// 從布局文件中獲取名叫tv_marry的文本視圖tv_marry = findViewById(R.id.tv_marry);// 從布局文件中獲取名叫rg_marry的單選組RadioGroup rg_marry = findViewById(R.id.rg_marry);// 給rg_marry設(shè)置單選監(jiān)聽器,一旦用戶點(diǎn)擊組內(nèi)的單選按鈕,就觸發(fā)監(jiān)聽器的onCheckedChanged方法rg_marry.setOnCheckedChangeListener(this); } // 在用戶點(diǎn)擊組內(nèi)的單選按鈕時(shí)觸發(fā) public void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId == R.id.rb_married) { tv_marry.setText('哇哦,祝你早生貴子');} else if (checkedId == R.id.rb_unmarried) { tv_marry.setText('哇哦,你的前途不可限量');} } }
Layout
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' android:padding='10dp' > <TextViewandroid:layout_width='match_parent'android:layout_height='wrap_content'android:text='請(qǐng)選擇您的婚姻狀況'android:textColor='#000000'android:textSize='17sp' /> <RadioGroupandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='vertical' > <!-- 通過button屬性修改單選按鈕的圖標(biāo) --><RadioButton android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:padding='5dp' android:button='@drawable/radio_selector' android:text='未婚' android:textColor='#000000' android:textSize='17sp' /> <!-- 通過drawableLeft屬性修改單選按鈕的圖標(biāo) --><RadioButton android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:padding='5dp' android:button='@null' android:drawableLeft='@drawable/radio_selector' android:drawablePadding='10dp' android:text='已婚' android:textColor='#000000' android:textSize='17sp' /> </RadioGroup> <TextViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:textColor='#000000'android:textSize='17sp' /></LinearLayout>
result
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. HTML DOM setInterval和clearInterval方法案例詳解2. html小技巧之td,div標(biāo)簽里內(nèi)容不換行3. XML入門精解之結(jié)構(gòu)與語法4. css進(jìn)階學(xué)習(xí) 選擇符5. XML入門的常見問題(一)6. 概述IE和SQL2k開發(fā)一個(gè)XML聊天程序7. 告別AJAX實(shí)現(xiàn)無刷新提交表單8. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)9. 使用css實(shí)現(xiàn)全兼容tooltip提示框10. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案
