Android控件RadioButton實現多選一功能
RadioButton實現多選一功能的方法,具體內容如下
一、簡介

二、RadioButton實現多選一方法
1、將多個RadioButton放在一個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里面取出每個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、檢查每個RadioButton是否被選取
if (radio.isChecked()) {
break;
}
4、取出被選取的那個RadioButton里面的值
Toast.makeText(Activity01.this, radio.getText(),
Toast.LENGTH_LONG).show();
三、代碼實例
效果圖:

代碼:
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實現多選一方法
* 1、將多個RadioButton放在一個RadioGroup里面
* 2、在RadioGroup里面取出每個RadioButton
* 3、檢查每個RadioButton是否被選取
* 4、取出被選取的那個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"
設置顏色,直接用#FFFFFF
2、
android:layout_gravity="center_horizontal"
文字居中顯示
3、
RadioButton在RadioGroup里面實現多選一
4、
android:background="@android:color/black"
設置黑色,系統(tǒng)自帶顏色
5、
int len = radioGroup1.getChildCount();
RadioGroup獲取孩子數量
6、
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
RadioGroup獲取孩子
7、
if (radio.isChecked())
判斷RadioButton是否被選取
8、
Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();
吐司
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android單選按鈕RadioButton的使用詳解
- Android開發(fā)設置RadioButton點擊效果的方法
- Android編程實現自定義PopupMenu樣式示例【顯示圖標與設置RadioButton圖標】
- Android RadioButton 圖片位置與大小實例詳解
- Android RadioGroup和RadioButton控件簡單用法示例
- Android中設置RadioButton在文字右邊的方法實例
- android RadioButton和CheckBox組件的使用方法
- Android RadioButton單選框的使用方法
- Android定制RadioButton樣式三種實現方法
- Android控件系列之RadioButton與RadioGroup使用方法
- Android控件RadioButton的使用方法
相關文章
android studio更新gradle錯誤構建項目失敗的解決方法
這篇文章主要介紹了android studio更新gradle錯誤構建項目失敗的解決方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

