Android實(shí)現(xiàn)單選按鈕
本文實(shí)例為大家分享了Android實(shí)現(xiàn)單選按鈕的具體代碼,供大家參考,具體內(nèi)容如下
單選按鈕
在默認(rèn)情況下,單選按鈕顯示為一個(gè)圓形圖標(biāo),可以在圖標(biāo)旁放一些說明文字。通常情況下RadioButton組件需要與RadioGroup組件一起使用,組成一個(gè)單選按鈕組。RadioGroup是可以容納多個(gè)RadioButton的容器。
<LinearLayout ? ? android:layout_width="match_parent" ? ? android:layout_height="60dp" ? ? android:orientation="horizontal"> ? ? <TextView ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:text="選擇性別:" ? ? ? ? android:textSize="25sp" ? ? ? ? android:gravity="center" ? ? ? ? android:textColor="@color/black"/> ? ? <RadioGroup ? ? ? ? android:id="@+id/radioGroup" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:orientation="horizontal" ? ? ? ? android:gravity="center"> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio_man" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="男" ? ? ? ? ? ? android:textColor="@color/black" ? ? ? ? ? ? android:textSize="25sp" ? ? ? ? ? ? android:checked="true"/> ? ? ? ? <RadioButton ? ? ? ? ? ? android:id="@+id/radio_female" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="女" ? ? ? ? ? ? android:textColor="@color/black" ? ? ? ? ? ? android:layout_marginLeft="100dp" ? ? ? ? ? ? android:textSize="25sp"/> ? ? </RadioGroup> </LinearLayout> <Button ? ? android:id="@+id/bt_submit" ? ? android:layout_width="100dp" ? ? android:layout_height="50dp" ? ? android:text="提交" ? ? android:textSize="20sp" ? ? android:layout_marginTop="10dp" ? ? android:layout_gravity="center"/>
布局效果顯示:

RadioButton組件的android:checked屬性用來指定選中的狀態(tài),android:checked="true"時(shí),表示選中;android:checked="false"時(shí),表示取消選中。
獲得選中的值有三種方法:
第一種是為RadioButton設(shè)置一個(gè)事件監(jiān)聽器setOnCheckChangeListener。
public class MainActivity extends AppCompatActivity {
? ? RadioGroup radioGroup;
? ??
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //初始化控件
? ? ? ? initView();
? ? ? ? //點(diǎn)擊事件
? ? ? ? clickEvent();
? ? }
? ? private void initView() {
? ? ? ? radioGroup = findViewById(R.id.radioGroup);
? ? }
? ? private void clickEvent() {
? ? ? ? //給RadioGroup綁定監(jiān)視器
? ? ? ? radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
? ? }
? ? //單選按鈕監(jiān)聽
? ? private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
? ? ? ? @Override
? ? ? ? public void onCheckedChanged(RadioGroup group, int checkedId) {
? ? ? ? ? ? RadioButton r = (RadioButton) findViewById(checkedId);//獲取被選中的Id
? ? ? ? ? ? Log.i("單選按鈕監(jiān)聽", "選擇性別為:" + r.getText().toString());
? ? ? ? }
? ? }
}單選按鈕監(jiān)聽日志效果:

第二種通過單擊其他按鈕獲取選中單選按鈕的值。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
? ? RadioGroup radioGroup;
? ? //提交
? ? Button bt_submit;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //初始化控件
? ? ? ? initView();
? ? ? ? //點(diǎn)擊事件
? ? ? ? clickEvent();
? ? }
? ? private void initView() {
? ? ? ? radioGroup = findViewById(R.id.radioGroup);
? ? ? ? bt_submit = findViewById(R.id.bt_submit);
? ? }
? ? private void clickEvent() {
? ? ? ? //提交
? ? ? ? bt_submit.setOnClickListener(this);
? ? }
? ? @Override
? ? public void onClick(View v) {
? ? ? ? switch (v.getId()) {
? ? ? ? ? ? case R.id.bt_submit:
? ? ? ? ? ? ? ? for (int i = 0; i < radioGroup.getChildCount(); i++) {
? ? ? ? ? ? ? ? ? ? RadioButton r = (RadioButton) radioGroup.getChildAt(i);
? ? ? ? ? ? ? ? ? ? if (r.isChecked()) {
? ? ? ? ? ? ? ? ? ? ? ? Log.i("單擊其他按鈕時(shí)獲取", "選擇性別為:" + r.getText());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
}判斷單擊其他按鈕獲取選中單選按鈕的值的日志效果展示:

第三種判斷被點(diǎn)擊的id是哪一個(gè)單選按鈕的id,通過id去獲取值。
public class MainActivity extends AppCompatActivity {
? ? RadioGroup radioGroup;
? ? //男
? ? RadioButton radio_man;
? ? //女
? ? RadioButton radio_female;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //初始化控件
? ? ? ? initView();
? ? ? ? //點(diǎn)擊事件
? ? ? ? clickEvent();
? ? }
? ? private void initView() {
? ? ? ? radioGroup = findViewById(R.id.radioGroup);
? ? ? ? radio_man = findViewById(R.id.radio_man);
? ? ? ? radio_female = findViewById(R.id.radio_female);
? ? }
? ? private void clickEvent() {
? ? ? ? //給RadioGroup綁定監(jiān)視器
? ? ? ? radioGroup.setOnCheckedChangeListener(new MyRadioButtonListener());
? ? }
? ? //單選按鈕監(jiān)聽
? ? private class MyRadioButtonListener implements RadioGroup.OnCheckedChangeListener {
? ? ? ? @Override
? ? ? ? public void onCheckedChanged(RadioGroup group, int checkedId) {
? ? ? ? ? ? // 選中狀態(tài)改變時(shí)被觸發(fā)
? ? ? ? ? ? switch (checkedId) {
? ? ? ? ? ? ? ? case R.id.radio_female:
? ? ? ? ? ? ? ? ? ? // 當(dāng)用戶選擇女性時(shí)
? ? ? ? ? ? ? ? ? ? Log.i("判斷點(diǎn)擊Id的單選按鈕", "選擇性別為:" + radio_female.getText().toString());
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case R.id.radio_man:
? ? ? ? ? ? ? ? ? ? // 當(dāng)用戶選擇男性時(shí)
? ? ? ? ? ? ? ? ? ? Log.i("判斷點(diǎn)擊Id的單選按鈕", "選擇性別為:"+radio_man.getText().toString());
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}判斷點(diǎn)擊的單選按鈕日志效果展示:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)自動(dòng)變換大小的ViewPager
- android?viewpager實(shí)現(xiàn)輪播效果
- Android使用ViewPager實(shí)現(xiàn)翻頁效果
- Android自定義View實(shí)現(xiàn)遙控器按鈕
- Android單選多選按鈕的使用方法
- Android 中使用RadioGroup和Fragment實(shí)現(xiàn)底部導(dǎo)航欄的功能
- Android基礎(chǔ)控件RadioGroup使用方法詳解
- Android RadioGroup多行顯示效果 解決單選問題
- Kotlin RadioGroup與ViewPager實(shí)現(xiàn)底層分頁按鈕方法
相關(guān)文章
Android鍵盤輸入語言設(shè)置默認(rèn)打開myanmar緬甸語的步驟
如何實(shí)現(xiàn)Android鍵盤輸入語言默認(rèn)打開為myanmar緬甸語,如果要設(shè)置某種語言在輸入法默認(rèn)打開可按一下步驟添加文件,我這里已經(jīng)驗(yàn)證時(shí)OK的2013-06-06
Android 官推 kotlin-first 的圖片加載庫——Coil的使用入門
這篇文章主要介紹了Android 官推 kotlin-first 的圖片加載庫——Coil的使用入門,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04
Android識(shí)別預(yù)裝的第三方App方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Android如何識(shí)別預(yù)裝的第三方App的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01
Android Studio實(shí)現(xiàn)格式化XML代碼順序
這篇文章主要介紹了Android Studio實(shí)現(xiàn)格式化XML代碼順序,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android UI實(shí)現(xiàn)廣告Banner輪播效果
這篇文章主要為大家詳細(xì)介紹了Android UI實(shí)現(xiàn)廣告Banner輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
詳解Android activity與fragment之間的通信交互
本篇文章主要介紹了詳解Android activity與fragment之間的通信交互,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android模擬器對(duì)應(yīng)的電腦快捷鍵說明
Android模擬器對(duì)應(yīng)的電腦快捷鍵說明,需要的朋友可以參考一下2013-06-06
Android實(shí)現(xiàn)上傳文件到服務(wù)器實(shí)例詳解
本篇文章詳細(xì)介紹了Android實(shí)現(xiàn)上傳文件到服務(wù)器實(shí)例詳解,實(shí)現(xiàn)了文件每隔5秒進(jìn)行上傳,有需要的可以了解一下。2016-11-11

