RadioGroup和RadioButton实现选项卡效果,selector,

本文介绍如何在Android中自定义RadioButton的样式,包括使用selector实现不同状态下的视觉效果,并探讨了RadioGroup的OnCheckedChangeListener与RadioButton的OnTouchListener的使用技巧及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:效果图


二:布局文件

<?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方法,可以根据这个特点执行特殊的业务需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值