对于单选框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()**函数。