Android控件RadioButton實(shí)現(xiàn)多選一功能
RadioButton實(shí)現(xiàn)多選一功能的方法,具體內(nèi)容如下
一、簡(jiǎn)介

二、RadioButton實(shí)現(xiàn)多選一方法
1、將多個(gè)RadioButton放在一個(gè)RadioGroup里面
<RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textColor="#FFFFFF" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textColor="#FFFFFF" /> </RadioGroup>
2、在RadioGroup里面取出每個(gè)RadioButton
public void onClick(View v) {
// TODO Auto-generated method stub
int len = radioGroup1.getChildCount();
for (int i = 0; i < len; i++) {
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11 }
}
3、檢查每個(gè)RadioButton是否被選取
if (radio.isChecked()) {
break;
}
4、取出被選取的那個(gè)RadioButton里面的值
Toast.makeText(Activity01.this, radio.getText(),
Toast.LENGTH_LONG).show();
三、代碼實(shí)例
效果圖:

代碼:
fry.Activity01
package fry;
import com.example.RadioButtonDemo1.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class Activity01 extends Activity {
private Button btn_chooseGender;
private RadioGroup radioGroup1;
private TextView tv_answer;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
tv_answer = (TextView) findViewById(R.id.tv_answer);
/*
* RadioButton實(shí)現(xiàn)多選一方法
* 1、將多個(gè)RadioButton放在一個(gè)RadioGroup里面
* 2、在RadioGroup里面取出每個(gè)RadioButton
* 3、檢查每個(gè)RadioButton是否被選取
* 4、取出被選取的那個(gè)RadioButton里面的值
*/
btn_chooseGender.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int len = radioGroup1.getChildCount();
for (int i = 0; i < len; i++) {
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
if (radio.isChecked()) {
Toast.makeText(Activity01.this, radio.getText(),
Toast.LENGTH_LONG).show();
break;
}
}
}
});
}
}
/RadioButtonDemo1/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性別" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_horizontal" android:textColor="#FFFFFF" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:textColor="#FFFFFF" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textColor="#FFFFFF" /> </RadioGroup> <Button android:id="@+id/btn_chooseGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇性別" android:textColor="#FFFFFF" /> /> <TextView android:id="@+id/tv_answer" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_gravity="center_horizontal" android:textColor="#FFFFFF" /> </LinearLayout>
四、收獲
1、
android:textColor="#FFFFFF"
設(shè)置顏色,直接用#FFFFFF
2、
android:layout_gravity="center_horizontal"
文字居中顯示
3、
RadioButton在RadioGroup里面實(shí)現(xiàn)多選一
4、
android:background="@android:color/black"
設(shè)置黑色,系統(tǒng)自帶顏色
5、
int len = radioGroup1.getChildCount();
RadioGroup獲取孩子數(shù)量
6、
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
RadioGroup獲取孩子
7、
if (radio.isChecked())
判斷RadioButton是否被選取
8、
Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();
吐司
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android單選按鈕RadioButton的使用詳解
- Android開(kāi)發(fā)設(shè)置RadioButton點(diǎn)擊效果的方法
- Android編程實(shí)現(xiàn)自定義PopupMenu樣式示例【顯示圖標(biāo)與設(shè)置RadioButton圖標(biāo)】
- Android RadioButton 圖片位置與大小實(shí)例詳解
- Android RadioGroup和RadioButton控件簡(jiǎn)單用法示例
- Android中設(shè)置RadioButton在文字右邊的方法實(shí)例
- android RadioButton和CheckBox組件的使用方法
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實(shí)現(xiàn)方法
- Android控件系列之RadioButton與RadioGroup使用方法
- Android控件RadioButton的使用方法
相關(guān)文章
Android編程實(shí)現(xiàn)將應(yīng)用強(qiáng)制安裝到手機(jī)內(nèi)存的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將應(yīng)用強(qiáng)制安裝到手機(jī)內(nèi)存的方法,分析了Android程序安裝的相關(guān)屬性設(shè)置技巧及注意事項(xiàng),需要的朋友可以參考下2015-12-12
android studio更新gradle錯(cuò)誤構(gòu)建項(xiàng)目失敗的解決方法
這篇文章主要介紹了android studio更新gradle錯(cuò)誤構(gòu)建項(xiàng)目失敗的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)直播點(diǎn)贊特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android嵌套滾動(dòng)與協(xié)調(diào)滾動(dòng)的實(shí)現(xiàn)方式匯總
如何實(shí)現(xiàn)這種協(xié)調(diào)滾動(dòng)的布局呢,我們使用CoordinatorLayout+AppBarLayout或者CoordinatorLayout+Behavior實(shí)現(xiàn),另一種方案是MotionLayout,我們看看都是怎么實(shí)現(xiàn)的吧2022-06-06
Android Okhttp請(qǐng)求查詢(xún)購(gòu)物車(chē)的實(shí)例代碼
下面小編就為大家分享一篇Android Okhttp請(qǐng)求查詢(xún)購(gòu)物車(chē)的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家介紹了Flutter?日歷組件簡(jiǎn)單實(shí)現(xiàn)的圖文示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

