当前位置:   article > 正文

单选按钮RadioButton_radiobuttonfor 单选框不信啊是

radiobuttonfor 单选框不信啊是

先来个例子感受一下

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
  3. android:layout_height="fill_parent" tools:context=".MainActivity"
  4. android:orientation="horizontal"
  5. >
  6. <TextView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:text="性别:"
  10. android:height="50px"/>
  11. <RadioGroup
  12. android:id="@+id/radioGroup1"
  13. android:orientation="horizontal"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content">
  16. <RadioButton
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:id="@+id/radio0"
  20. android:text="男"
  21. android:checked="true"/>
  22. <RadioButton
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:id="@+id/radio1"
  26. android:text="女"/>
  27. </RadioGroup>
  28. <Button
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:id="@+id/button1"
  32. android:text="提交"/>
  33. </LinearLayout>

有图有真相



1、在改变单选按钮组的值时获取

  1. RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
  2. sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  3. @Override
  4. public void onCheckedChanged(RadioGroup group, int checkedId) {
  5. RadioButton r = (RadioButton)findViewById(checkedId);
  6. r.getText(); //获取被选中的单选按钮的值
  7. }
  8. });

2、单击其他按钮时获

  1. final RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
  2. Button button = (Button)findViewById(R.id.button1);
  3. button.setOnClickListener(new View.OnClickListener() {
  4. @Override
  5. public void onClick(View v) {
  6. for(int i=0;i<sex.getChildCount();i++){
  7. RadioButton r = (RadioButton)sex.getChildAt(i); //根据索引值获取单选按钮
  8. if(r.isChecked()){ //判断单选按钮是否被选中
  9. r.getText(); //获取被选中的单选按钮的值
  10. break; //跳出for循环
  11. }
  12. }
  13. }
  14. });


实例上的java代码

  1. protected void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.activity_main);
  4. final RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
  5. sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  6. @Override
  7. public void onCheckedChanged(RadioGroup group, int checkedId) {
  8. RadioButton r = (RadioButton)findViewById(checkedId);//获取被选择的单选按钮
  9. Log.i("单选按钮","您选择的是:"+r.getText());
  10. }
  11. });
  12. Button button = (Button)findViewById(R.id.button1); //获取提交按钮
  13. button.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View v) {
  16. for(int i=0;i<sex.getChildCount();i++){
  17. RadioButton r = (RadioButton)sex.getChildAt(i);
  18. if(r.isChecked()){ //判断单选按钮是否被选中
  19. Log.i("单选按钮","性别:"+r.getText());
  20. break; //跳出for循环
  21. }
  22. }
  23. }
  24. });
  25. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/675863
推荐阅读
  

闽ICP备14008679号