Android第六天

CheckBox控件

一、CheckBox属性
1.实现多选的控件
2.可设置初始状态是否被选中
android:checked=”true”
二、使用
1.初始化

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="true"
    android:text="吃火锅" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="true"
    android:text="吃烧烤" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="true"
    android:text="吃鸡" />

private CheckBox checkBox1;
checkBox1 = (CheckBox)findViewById(R.id.checkBox1);

2.设置监听器

checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        if(isChecked){
            String text = checkBox1.getText().toString(); 
            //获得CheckedBox的文本内容
            Log.i("tag",text);
            }
       }
});

3.效果
这里写图片描述

RadioGroup和RadioButton控件

一、RadioGroup是RadioButton的一个集合,提供多选一机制
二、属性
android:orientation=”vertical”——垂直排布
android:orientation=”horizontal”——水平排布
三、使用
1.将数个RadioButton放到同一个RadioGroup中,设置orientation为水平排布

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


<RadioButton
    android:id="@+id/radioButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:checked="true"
    android:text="男" />

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="女" />
</RadioGroup>

2.初始化RadioButton并设置监听器

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rg = (RadioGroup) findViewById(R.id.RadioGroup1);
        rg.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
        switch (checkedId){
            case R.id.radioButton1:
                Log.i("tag","man");break;
            case R.id.radioButton2:
                Log.i("tag","woman");break;
        }
    }
}

3.效果
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值