一:效果图
二:布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical" >
<include layout="@layout/main_head_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/tab_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
<RadioGroup
android:id="@+id/tab_radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- checked="true"是指默认被选中
android:button="@null" 取消RaidoButton原来的圆按钮样式
类似的还有Android:background="@null":即控件自带的背景设为空 -->
<RadioButton
android:id="@+id/radio_1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:background="@drawable/tab_rb1" />
<RadioButton
android:id="@+id/radio_2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/tab_rb2" />
<RadioButton
android:id="@+id/radio_3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/tab_rb3" />
<RadioButton
android:id="@+id/radio_4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:background="@drawable/tab_rb4" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
三:RadioButton对应的selector效果
存放路径drawable/**.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
android:state_pressed="true" 控件按下去的效果
android:state_focused="true" 控件获取到光标时的效果
android:state_checked="true" 控件被选中时的效果
<item android:drawable="@drawable/radio_acc_up"/> 默认的控件显示时的效果
-->
<item android:drawable="@drawable/radio_acc_on" android:state_pressed="true" />
<item android:drawable="@drawable/radio_acc_on" android:state_focused="true" />
<item android:drawable="@drawable/radio_acc_on" android:state_checked="true" />
<item android:drawable="@drawable/radio_acc_up"/>
</selector>
四:RadioGroup的OnCheckedChangeListener和RadoButton的OnTouchListener使用的注意事项
4.1:RadioGroup的OnCheckedChangeeListener
tab_rGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//onCheckedChanged的第一个参数是当前的RadioGroup,第二个参数是被选中的RadioButton的id
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.radio_1:
break;
default:
break;
}
}
});
4.2:RadoButton的OnTouchListener
radio1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//v就是所touch的控件
//可以根据不同的event,处理不同的业务逻辑
//MotionEvent.ACTION_DOWN://单机到控件v就执行
//MotionEvent.ACTION_MOVE://触摸并滑动控件v时执行
//MotionEvent.ACTION_UP://离开控件v时执行
return true;
});
当RadioGroup的OnCheckedChangeListener和RadioButton的OnTouchListener同时使用时,即某个RadioButton(这里是radio1)即处于OnCheckedChangeListener的onCheckedChanged方法中又处于OnTouchListener的onTouch方法中时,会优先执行onTouch方法,当onTouch方法return true时,onTouch事件结束,不会再执行onCheckedChanged方法;当onTouch方法return false时,onTouch事件尚未结束,会紧接着执行onCheckedChanged方法,可以根据这个特点执行特殊的业务需求。