您的位置:首页 > 移动开发 > Android开发

Android RadioGroup及RadioButton的使用方法

2015-12-02 10:23 731 查看
效果图:



1、RadioGroup的布局文件

<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/ll_title"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="5dp"
android:orientation="horizontal" >

<RadioButton
android:id="@+id/rb_1"
android:layout_width="95dp"
android:layout_height="match_parent"
android:background="@drawable/top_button_selector"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="待办"
android:textColor="@drawable/top_text_selector"
android:textSize="14dp" />

<RadioButton
android:id="@+id/rb_2"
android:layout_width="95dp"
android:layout_height="match_parent"
android:background="@drawable/top_button_selector_1"
android:button="@null"
android:gravity="center"
android:text="已办"
android:textColor="@drawable/top_text_selector"
android:textSize="14dp" />
</RadioGroup>

背景选择器:名称为top_button_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/top_tab_01_bg_hover" android:state_checked="true"></item>
<item android:drawable="@drawable/top_tab_02_bg" android:state_checked="false"></item>
</selector>


字体选择器:名称为top_text_selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true"></item>
<item android:color="@color/gray_text" android:state_checked="false"></item>
</selector>


2、在Activity中设置监听器,并进行相应的操作

rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.rb_1:
mCurrentType = "undone";
gdAdapter.addData(notDealgongDanInfoArr, mCurrentType);
gdAdapter.notifyDataSetChanged();
break;
case R.id.rb_2:
mCurrentType = "done";
gdAdapter.addData(dealgongDanInfoArr, mCurrentType);
gdAdapter.notifyDataSetChanged();
break;
default:
break;
}
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: