RadioButton實現多選一
阿新 • • 發佈:2017-08-05
apk cnblogs lac activit round find -a parent roi
RadioButton實現多選一
一、簡介
二、RadioButton實現多選一方法
1、將多個RadioButton放在一個RadioGroup裏面
1 <RadioGroup 2 android:id="@+id/radioGroup1" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" > 5 6 <RadioButton 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:text="男" 10 android:textColor="#FFFFFF" /> 11 12 <RadioButton 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="女" 16 android:textColor="#FFFFFF" /> 17 </RadioGroup>
2、在RadioGroup裏面取出每個RadioButton
1 public void onClick(View v) { 2 // TODO Auto-generated method stub 3 int len = radioGroup1.getChildCount(); 4 for (int i = 0; i < len; i++) {5 RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11 } 12 }
3、檢查每個RadioButton是否被選取
1 if (radio.isChecked()) {
4 break; 5 }
4、取出被選取的那個RadioButton裏面的值
1 Toast.makeText(Activity01.this, radio.getText(), 2 Toast.LENGTH_LONG).show();
三、代碼實例
效果圖:
代碼:
fry.Activity01
1 package fry; 2 3 import com.example.RadioButtonDemo1.R; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.RadioButton; 11 import android.widget.RadioGroup; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 15 public class Activity01 extends Activity { 16 private Button btn_chooseGender; 17 private RadioGroup radioGroup1; 18 private TextView tv_answer; 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 // TODO Auto-generated method stub 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity01); 25 26 btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender); 27 radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); 28 tv_answer = (TextView) findViewById(R.id.tv_answer); 29 /* 30 * RadioButton實現多選一方法 31 * 1、將多個RadioButton放在一個RadioGroup裏面 32 * 2、在RadioGroup裏面取出每個RadioButton 33 * 3、檢查每個RadioButton是否被選取 34 * 4、取出被選取的那個RadioButton裏面的值 35 */ 36 btn_chooseGender.setOnClickListener(new OnClickListener() { 37 38 @Override 39 public void onClick(View v) { 40 // TODO Auto-generated method stub 41 int len = radioGroup1.getChildCount(); 42 for (int i = 0; i < len; i++) { 43 RadioButton radio = (RadioButton) radioGroup1.getChildAt(i); 44 if (radio.isChecked()) { 45 Toast.makeText(Activity01.this, radio.getText(), 46 Toast.LENGTH_LONG).show(); 47 break; 48 } 49 } 50 } 51 }); 52 } 53 }
/RadioButtonDemo1/res/layout/activity01.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:background="@android:color/black" 6 android:orientation="vertical" > 7 8 <TextView 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="性別" 12 android:textAppearance="?android:attr/textAppearanceLarge" 13 android:layout_gravity="center_horizontal" 14 android:textColor="#FFFFFF" /> 15 16 <RadioGroup 17 android:id="@+id/radioGroup1" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content" > 20 21 <RadioButton 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="男" 25 android:textColor="#FFFFFF" /> 26 27 <RadioButton 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="女" 31 android:textColor="#FFFFFF" /> 32 </RadioGroup> 33 34 <Button 35 android:id="@+id/btn_chooseGender" 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content" 38 android:text="選擇性別" 39 android:textColor="#FFFFFF" /> 40 /> 41 42 <TextView 43 android:id="@+id/tv_answer" 44 android:layout_width="match_parent" 45 android:layout_height="wrap_content" 46 android:text="" 47 android:textAppearance="?android:attr/textAppearanceLarge" 48 android:layout_gravity="center_horizontal" 49 android:textColor="#FFFFFF" /> 50 </LinearLayout>
四、收獲
1、
android:textColor="#FFFFFF"
設置顏色,直接用#FFFFFF
2、
android:layout_gravity="center_horizontal"
文字居中顯示
3、
RadioButton在RadioGroup裏面實現多選一
4、
android:background="@android:color/black"
設置黑色,系統自帶顏色
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();
吐司
RadioButton實現多選一