当前位置:   article > 正文

Android studio中Radiobutton 和 Radiogroup的使用详解_androidstudio单选按钮

androidstudio单选按钮

1.Radiobutton 和 Radiogroup的解析(单选框)

Radiobutton 是一个单选按钮,它表示一组选项中的一个。它通常与其他 Radiobutton 控件组合在一起,形成一个 Radiogroup。

Radiogroup 是一个容器控件,用于将多个 Radiobutton 组织在一起。Radiogroup 保证了其中的 Radiobutton 只能选择一个,即单选功能。 注意:Radiogroup具有和LinearLayout一样的横向属性(android:orientation="horizontal")和垂直属性(android:orientation="vertical")

2.Radiobutton的常用属性:

  • android:enabled:指定 Radiobutton 是否可用,值为 "true" 或 "false"
  • android:checked:指定 Radiobutton 的初始选中状态,值为 "true" 或 "false"

3.示例代码效果

  1. <RadioButton
  2. android:layout_width="wrap_content"
  3. android:layout_height="wrap_content"
  4. android:text="单选"/>
  5. <RadioGroup
  6. android:id="@+id/sex"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="horizontal">
  10. <RadioButton
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="男"
  14. android:checked="true"
  15. android:id="@+id/sex_boy"/>
  16. <RadioButton
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="女"
  20. android:id="@+id/sex_girl"/>
  21. </RadioGroup>
  1. findcheck();
  2. sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  3. @Override
  4. public void onCheckedChanged(RadioGroup group, int checkedId) {
  5. switch (checkedId){
  6. case R.id.sex_boy:
  7. Toast.makeText(RadiogroupActivity.this,"男", Toast.LENGTH_SHORT).show();
  8. break;
  9. case R.id.sex_girl:
  10. Toast.makeText(RadiogroupActivity.this,"女", Toast.LENGTH_SHORT).show();
  11. break;
  12. }
  13. }
  14. });
  15. }
  16. private void findcheck(){
  17. sex=findViewById(R.id.sex);
  18. }

4.效果图展示

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

闽ICP备14008679号