容易遗忘的单选框RadioButton的监听使用

对于单选框RadioButton,若想不通过button的OnClickListener进行监听,而是直接监听单选框的选择同步做判断,可以使用OnCheckedChangeListener来进行判断已选的是哪一个RadioButton,具体写法如下
此处为百度地图显示地图种类的判别的选择demo,当点击不同radioBurtton时地图将进行同步切换
布局文件如下

<RadioGroup
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <RadioButton
            android:id="@+id/normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通图"/>
        <RadioButton
            android:id="@+id/satellite"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="卫星图"
            android:layout_marginLeft="70dp"/>
    </RadioGroup>

java文件如下(监听部分)

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(checkedId == R.id.satellite){//注意此处id的写法
                    mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
                }else{
                    mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
                }
            }
        });

多选框checkbox的监听大致相同,需要注意的是,单选框需要radioGroup来对各个radioButton进行管理。
同时,若需要判断是否已选可以调用**isChecked()**函数。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值