Android基础控件单选按钮RadioButton和Checkbox复选按钮的使用

1、相关简介

  RadioButton需要和RadioGroup结合使用,在RadioGroup设置布局方式!

  Checkbox是单独使用,本文为了方便放在了RadioGroup中!

 

2、简单使用

  方法说明:

final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.getChildCount() //获取子控件数量
RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i); //获取子控件
radioButton.isChecked() //获取按钮选择状态
radioButton.getText() //获取按钮文本

  xml布局文件

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioGroup"
            android:orientation="horizontal">
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio11"
                    android:text="男"
                    android:checked="true"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio22"
                    android:text="女"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/radio33"
                    android:text="太监"/>
        </RadioGroup>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radioBtn"
            android:text="提交"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/boxGroup"
            android:orientation="vertical">
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/box11"
                    android:text="男"
                    />
                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/box22"
                    android:text="女"/>
        </RadioGroup>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/boxBtn"
            android:text="提交"/>

  Java文件:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        // Set up the login form.

        final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
//        方法一
//        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int i) {
//                RadioButton radioButton = (RadioButton)findViewById(i);
//                Toast.makeText(getApplicationContext(),"你选择了"+radioButton.getText(),Toast.LENGTH_LONG).show();
//            }
//        });

//        方法二
        Button button = (Button)findViewById(R.id.radioBtn);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0;i < radioGroup.getChildCount();i++){
                    RadioButton radioButton = (RadioButton)radioGroup.getChildAt(i);
                    if (radioButton.isChecked()){
                        Toast.makeText(getApplicationContext(),"你选择了"+radioButton.getText(),Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });

        Button button1 = (Button)findViewById(R.id.boxBtn);
        final RadioGroup boxGroup = (RadioGroup)findViewById(R.id.boxGroup);
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String choos = "";
                for (int i=0;i<boxGroup.getChildCount();i++){
                    CheckBox checkBox = (CheckBox)boxGroup.getChildAt(i);
                    if (checkBox.isChecked()){
                        choos +=checkBox.getText().toString();
                    }else {
                        if (choos.indexOf(checkBox.getText().toString())!=-1){
                            choos.substring(choos.indexOf(checkBox.getText().toString()));
                        }
                    }
                    if (i==boxGroup.getChildCount()-1){
                        Toast.makeText(getApplicationContext(),"选了"+choos,Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

    }
}

效果图

 

转载于:https://www.cnblogs.com/xianfeng-zhang/p/8080922.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值