高仿快递100--实战之RadioGroup和RadioButton应用

1.RadioButton和CheckBox的区别:

a.单个RadioButton在选中后,通过点击无法变为未选中

    单个CheckBox在选中后,通过点击可以变为未选中

b.一组RadioButton,只能同时选中一个

     一组CheckBox,能同时选中多个

c.RadioButton在大部分UI框架中默认都以圆形表示

      CheckBox在大部分UI框架中默认都以矩形表示

2.RadioButton和RadioGroup的关系:

a.RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

b.每个RadioGroup中的RadioButton同时只能有一个被选中

c.不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

d.大部分场合下,一个RadioGroup中至少有2个RadioButton

e.大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

3.简单介绍完后,先来看一下本应用中的效果图吧:

简单的一个弹出pop,然后里面提供了四种订单的状态,实现起来也不难,闲来看一下xml代码吧:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/rl_order_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@id/top_arrow"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/height_title_bar" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/btn_sort_arrow" />
    </RelativeLayout>

    <RadioGroup
        android:id="@+id/btn_sort"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top_arrow"
        android:background="@drawable/btn_sort_bg"
        android:checkedButton="@id/btn_sort_unsigned"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="10.0dip" >

        <RadioButton
            android:id="@+id/btn_sort_all"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="#00000000"
            android:button="@null"
            android:clickable="true"
            android:drawableTop="@drawable/btn_sort_all_selector"
            android:gravity="center"
            android:scaleType="matrix"
            android:text="@string/btn_sort_all"
            android:textColor="@color/grey_878787" />

        <RadioButton
            android:id="@+id/btn_sort_unsigned"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="#00000000"
            android:button="@null"
            android:clickable="true"
            android:drawableTop="@drawable/btn_sort_unsigned_selector"
            android:gravity="center"
            android:scaleType="matrix"
            android:text="@string/btn_sort_unsigned"
            android:textColor="@color/blue_kuaidi100" />

        <RadioButton
            android:id="@+id/btn_sort_signed"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="#00000000"
            android:button="@null"
            android:clickable="true"
            android:drawableTop="@drawable/btn_sort_signed_selector"
            android:gravity="center"
            android:scaleType="matrix"
            android:text="@string/btn_sort_signed"
            android:textColor="@color/grey_878787" />

        <RadioButton
            android:id="@+id/btn_sort_recycle"
            android:layout_width="0.0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.0"
            android:background="#00000000"
            android:button="@null"
            android:clickable="true"
            android:drawableTop="@drawable/btn_sort_recycle_selector"
            android:gravity="center"
            android:scaleType="matrix"
            android:text="@string/btn_sort_recycle"
            android:textColor="@color/grey_878787" />
    </RadioGroup>

    <View
        android:id="@+id/btn_grey_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/btn_sort"
        android:background="@color/black_7000" />

</RelativeLayout>
从xml布局文件中,看出使用了radiogroup,并且包含了四个radiobutton。这样便可以实现出那个弹出的popUpWindow.并且在RadioButton的图片中使用了状态选择器,分别在按下 选中和正常状态下,显示三种不同色值得图片,以一个为例,如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_sort_signed_pressed" android:state_pressed="true"/>
    <item android:drawable="@drawable/btn_sort_signed_selected" android:state_checked="true"/>
    <item android:drawable="@drawable/btn_sort_signed_normal"/>

</selector>
java代码如下,代码里都有注释,目测可以看明白:

/**
	 * 显示签收状态的pop
	 */
	private void showOrderStatusPop() {
		if (orderStatusPopView == null) {
			orderStatusPopView = View.inflate(mContext, R.layout.pop_bill_sort,
					null);
		}
		initOrderStatusPopView();
		setCurrentCheckedItem();
		setOrderStatusPopViewListener();
		if (orderStatusPopupWindow == null) {
			orderStatusPopupWindow = new PopupWindow(orderStatusPopView,
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
			// 使其不聚集
			orderStatusPopupWindow.setFocusable(false);
			// 设置允许在外点击消失
			orderStatusPopupWindow.setOutsideTouchable(true);
			// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
			orderStatusPopupWindow.setBackgroundDrawable(new BitmapDrawable());
		}
		int i = DensityUtil.dip2px(mContext, 60.0f);
		iv_arrow.setImageResource(R.drawable.arrow_up_float);
		orderStatusPopupWindow.showAsDropDown(top, 0, -i);
	}

	/**
	 * 初始化订单状态控件
	 */
	private void initOrderStatusPopView() {
		rl_order_parent = (RelativeLayout) orderStatusPopView
				.findViewById(R.id.rl_order_parent);
		btn_sort = (RadioGroup) orderStatusPopView.findViewById(R.id.btn_sort);
		btn_sort_all = (RadioButton) orderStatusPopView
				.findViewById(R.id.btn_sort_all);
		btn_sort_unsigned = (RadioButton) orderStatusPopView
				.findViewById(R.id.btn_sort_unsigned);
		btn_sort_signed = (RadioButton) orderStatusPopView
				.findViewById(R.id.btn_sort_signed);
		btn_sort_recycle = (RadioButton) orderStatusPopView
				.findViewById(R.id.btn_sort_recycle);

	}

	/**
	 * 设置订单状态的监听
	 */
	private void setOrderStatusPopViewListener() {
		rl_order_parent.setOnClickListener(this);
		btn_sort_all.setOnClickListener(this);
		btn_sort_unsigned.setOnClickListener(this);
		btn_sort_signed.setOnClickListener(this);
		btn_sort_recycle.setOnClickListener(this);
		//给radioGroup设置选中变化的监听,便于检测当前选中了哪个,方便下次再次显示回显
		btn_sort.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.btn_sort_all:
					currentCheckedId = checkedId;
					break;
				case R.id.btn_sort_unsigned:
					currentCheckedId = checkedId;
					break;
				case R.id.btn_sort_signed:
					currentCheckedId = checkedId;
					break;
				case R.id.btn_sort_recycle:
					currentCheckedId = checkedId;
					break;
				}

			}
		});
		btn_sort.check(currentCheckedId);
	}

	/**
	 * 设置当前显示的状态
	 */
	private void setCurrentCheckedItem() {
		switch (currentCheckedId) {
		case R.id.btn_sort_all:
			resetTextColor();
			btn_sort_all.setTextColor(mContext.getResources().getColor(
					R.color.blue_kuaidi100));
			break;
		case R.id.btn_sort_unsigned:
			resetTextColor();
			btn_sort_unsigned.setTextColor(mContext.getResources().getColor(
					R.color.blue_kuaidi100));
			break;
		case R.id.btn_sort_signed:
			resetTextColor();
			btn_sort_signed.setTextColor(mContext.getResources().getColor(
					R.color.blue_kuaidi100));
			break;
		case R.id.btn_sort_recycle:
			resetTextColor();
			btn_sort_recycle.setTextColor(mContext.getResources().getColor(
					R.color.blue_kuaidi100));
			break;
		}

	}

	/**
	 * 重置文字颜色
	 */
	private void resetTextColor() {
		btn_sort_all.setTextColor(mContext.getResources().getColor(
				R.color.grey_878787));
		btn_sort_unsigned.setTextColor(mContext.getResources().getColor(
				R.color.grey_878787));
		btn_sort_signed.setTextColor(mContext.getResources().getColor(
				R.color.grey_878787));
		btn_sort_recycle.setTextColor(mContext.getResources().getColor(
				R.color.grey_878787));

	}

这样,便实现了一个pop的弹出和radioGroup radioButton的一个结合,其实在实际应用中,底部使用RadioGroup的应用也不少,大同小异,关于这个小知识点就介绍到这了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值