RadioGroup与Radio、复选框CheckBox

获取RadioGroup与Radio、复选框CheckBox中的内容

Radio和CheckBox都是提供我们进行选择的控件,不同的是CheckBox是可以进行多选的,Radio只能进行单选,其实都很简单,本来是不想给自己做这个笔记的,但是想想做事应该有头有尾还是写了下面的小例子

复选框CheckBox

<CheckBox
    android:id="@+id/checkbox"
    android:checked="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="排球"/>

    <CheckBox
        android:id="@+id/checkbox2"
        android:checked="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球"/>

这里唯一需要注意的就是checked,设置为true是选中,false是没有选中

public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener{
    private CheckBox checkBox;
    private CheckBox checkBox2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkBox = (CheckBox) findViewById(R.id.checkbox);
        checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
        checkBox.setOnCheckedChangeListener(this);
        checkBox2.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(checkBox.isChecked()){
            Toast.makeText(this,"选中的是:"+ checkBox.getText().toString(),Toast.LENGTH_SHORT).show();
        }
        if(checkBox2.isChecked()){
            Toast.makeText(this,"选中的是:"+ checkBox2.getText().toString(),Toast.LENGTH_SHORT).show();
        }
    }
}

Radio与RadioGroup

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="女"/>
    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="男"
        />
</RadioGroup>
public class MainActivity extends Activity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroup;
    private RadioButton radio1, radio2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radioGroup.setOnCheckedChangeListener(this);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == radio1.getId()) {
            Toast.makeText(this, "你的性别是:" + radio1.getText().toString(), Toast.LENGTH_SHORT).show();
        }
        if (checkedId == radio2.getId()) {
            Toast.makeText(this, "你的性别是:" + radio2.getText().toString(), Toast.LENGTH_SHORT).show();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值