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

来源:互联网 发布:昆明行知中学宿舍 编辑:程序博客网 时间:2024/05/16 11:17
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代码吧:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:id="@+id/rl_order_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <RelativeLayout  
  9.         android:id="@id/top_arrow"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="@dimen/height_title_bar" >  
  12.   
  13.         <ImageView  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:layout_alignParentBottom="true"  
  17.             android:layout_centerHorizontal="true"  
  18.             android:src="@drawable/btn_sort_arrow" />  
  19.     </RelativeLayout>  
  20.   
  21.     <RadioGroup  
  22.         android:id="@+id/btn_sort"  
  23.         android:layout_width="fill_parent"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_below="@id/top_arrow"  
  26.         android:background="@drawable/btn_sort_bg"  
  27.         android:checkedButton="@id/btn_sort_unsigned"  
  28.         android:gravity="center"  
  29.         android:orientation="horizontal"  
  30.         android:padding="10.0dip" >  
  31.   
  32.         <RadioButton  
  33.             android:id="@+id/btn_sort_all"  
  34.             android:layout_width="0.0dip"  
  35.             android:layout_height="wrap_content"  
  36.             android:layout_weight="1.0"  
  37.             android:background="#00000000"  
  38.             android:button="@null"  
  39.             android:clickable="true"  
  40.             android:drawableTop="@drawable/btn_sort_all_selector"  
  41.             android:gravity="center"  
  42.             android:scaleType="matrix"  
  43.             android:text="@string/btn_sort_all"  
  44.             android:textColor="@color/grey_878787" />  
  45.   
  46.         <RadioButton  
  47.             android:id="@+id/btn_sort_unsigned"  
  48.             android:layout_width="0.0dip"  
  49.             android:layout_height="wrap_content"  
  50.             android:layout_weight="1.0"  
  51.             android:background="#00000000"  
  52.             android:button="@null"  
  53.             android:clickable="true"  
  54.             android:drawableTop="@drawable/btn_sort_unsigned_selector"  
  55.             android:gravity="center"  
  56.             android:scaleType="matrix"  
  57.             android:text="@string/btn_sort_unsigned"  
  58.             android:textColor="@color/blue_kuaidi100" />  
  59.   
  60.         <RadioButton  
  61.             android:id="@+id/btn_sort_signed"  
  62.             android:layout_width="0.0dip"  
  63.             android:layout_height="wrap_content"  
  64.             android:layout_weight="1.0"  
  65.             android:background="#00000000"  
  66.             android:button="@null"  
  67.             android:clickable="true"  
  68.             android:drawableTop="@drawable/btn_sort_signed_selector"  
  69.             android:gravity="center"  
  70.             android:scaleType="matrix"  
  71.             android:text="@string/btn_sort_signed"  
  72.             android:textColor="@color/grey_878787" />  
  73.   
  74.         <RadioButton  
  75.             android:id="@+id/btn_sort_recycle"  
  76.             android:layout_width="0.0dip"  
  77.             android:layout_height="wrap_content"  
  78.             android:layout_weight="1.0"  
  79.             android:background="#00000000"  
  80.             android:button="@null"  
  81.             android:clickable="true"  
  82.             android:drawableTop="@drawable/btn_sort_recycle_selector"  
  83.             android:gravity="center"  
  84.             android:scaleType="matrix"  
  85.             android:text="@string/btn_sort_recycle"  
  86.             android:textColor="@color/grey_878787" />  
  87.     </RadioGroup>  
  88.   
  89.     <View  
  90.         android:id="@+id/btn_grey_view"  
  91.         android:layout_width="fill_parent"  
  92.         android:layout_height="fill_parent"  
  93.         android:layout_below="@id/btn_sort"  
  94.         android:background="@color/black_7000" />  
  95.   
  96. </RelativeLayout>  
从xml布局文件中,看出使用了radiogroup,并且包含了四个radiobutton。这样便可以实现出那个弹出的popUpWindow.并且在RadioButton的图片中使用了状态选择器,分别在按下 选中和正常状态下,显示三种不同色值得图片,以一个为例,如下:

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <item android:drawable="@drawable/btn_sort_signed_pressed" android:state_pressed="true"/>  
  5.     <item android:drawable="@drawable/btn_sort_signed_selected" android:state_checked="true"/>  
  6.     <item android:drawable="@drawable/btn_sort_signed_normal"/>  
  7.   
  8. </selector>  
java代码如下,代码里都有注释,目测可以看明白:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 显示签收状态的pop 
  3.      */  
  4.     private void showOrderStatusPop() {  
  5.         if (orderStatusPopView == null) {  
  6.             orderStatusPopView = View.inflate(mContext, R.layout.pop_bill_sort,  
  7.                     null);  
  8.         }  
  9.         initOrderStatusPopView();  
  10.         setCurrentCheckedItem();  
  11.         setOrderStatusPopViewListener();  
  12.         if (orderStatusPopupWindow == null) {  
  13.             orderStatusPopupWindow = new PopupWindow(orderStatusPopView,  
  14.                     LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  15.             // 使其不聚集  
  16.             orderStatusPopupWindow.setFocusable(false);  
  17.             // 设置允许在外点击消失  
  18.             orderStatusPopupWindow.setOutsideTouchable(true);  
  19.             // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
  20.             orderStatusPopupWindow.setBackgroundDrawable(new BitmapDrawable());  
  21.         }  
  22.         int i = DensityUtil.dip2px(mContext, 60.0f);  
  23.         iv_arrow.setImageResource(R.drawable.arrow_up_float);  
  24.         orderStatusPopupWindow.showAsDropDown(top, 0, -i);  
  25.     }  
  26.   
  27.     /** 
  28.      * 初始化订单状态控件 
  29.      */  
  30.     private void initOrderStatusPopView() {  
  31.         rl_order_parent = (RelativeLayout) orderStatusPopView  
  32.                 .findViewById(R.id.rl_order_parent);  
  33.         btn_sort = (RadioGroup) orderStatusPopView.findViewById(R.id.btn_sort);  
  34.         btn_sort_all = (RadioButton) orderStatusPopView  
  35.                 .findViewById(R.id.btn_sort_all);  
  36.         btn_sort_unsigned = (RadioButton) orderStatusPopView  
  37.                 .findViewById(R.id.btn_sort_unsigned);  
  38.         btn_sort_signed = (RadioButton) orderStatusPopView  
  39.                 .findViewById(R.id.btn_sort_signed);  
  40.         btn_sort_recycle = (RadioButton) orderStatusPopView  
  41.                 .findViewById(R.id.btn_sort_recycle);  
  42.   
  43.     }  
  44.   
  45.     /** 
  46.      * 设置订单状态的监听 
  47.      */  
  48.     private void setOrderStatusPopViewListener() {  
  49.         rl_order_parent.setOnClickListener(this);  
  50.         btn_sort_all.setOnClickListener(this);  
  51.         btn_sort_unsigned.setOnClickListener(this);  
  52.         btn_sort_signed.setOnClickListener(this);  
  53.         btn_sort_recycle.setOnClickListener(this);  
  54.         //给radioGroup设置选中变化的监听,便于检测当前选中了哪个,方便下次再次显示回显  
  55.         btn_sort.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  56.   
  57.             @Override  
  58.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  59.                 switch (checkedId) {  
  60.                 case R.id.btn_sort_all:  
  61.                     currentCheckedId = checkedId;  
  62.                     break;  
  63.                 case R.id.btn_sort_unsigned:  
  64.                     currentCheckedId = checkedId;  
  65.                     break;  
  66.                 case R.id.btn_sort_signed:  
  67.                     currentCheckedId = checkedId;  
  68.                     break;  
  69.                 case R.id.btn_sort_recycle:  
  70.                     currentCheckedId = checkedId;  
  71.                     break;  
  72.                 }  
  73.   
  74.             }  
  75.         });  
  76.         btn_sort.check(currentCheckedId);  
  77.     }  
  78.   
  79.     /** 
  80.      * 设置当前显示的状态 
  81.      */  
  82.     private void setCurrentCheckedItem() {  
  83.         switch (currentCheckedId) {  
  84.         case R.id.btn_sort_all:  
  85.             resetTextColor();  
  86.             btn_sort_all.setTextColor(mContext.getResources().getColor(  
  87.                     R.color.blue_kuaidi100));  
  88.             break;  
  89.         case R.id.btn_sort_unsigned:  
  90.             resetTextColor();  
  91.             btn_sort_unsigned.setTextColor(mContext.getResources().getColor(  
  92.                     R.color.blue_kuaidi100));  
  93.             break;  
  94.         case R.id.btn_sort_signed:  
  95.             resetTextColor();  
  96.             btn_sort_signed.setTextColor(mContext.getResources().getColor(  
  97.                     R.color.blue_kuaidi100));  
  98.             break;  
  99.         case R.id.btn_sort_recycle:  
  100.             resetTextColor();  
  101.             btn_sort_recycle.setTextColor(mContext.getResources().getColor(  
  102.                     R.color.blue_kuaidi100));  
  103.             break;  
  104.         }  
  105.   
  106.     }  
  107.   
  108.     /** 
  109.      * 重置文字颜色 
  110.      */  
  111.     private void resetTextColor() {  
  112.         btn_sort_all.setTextColor(mContext.getResources().getColor(  
  113.                 R.color.grey_878787));  
  114.         btn_sort_unsigned.setTextColor(mContext.getResources().getColor(  
  115.                 R.color.grey_878787));  
  116.         btn_sort_signed.setTextColor(mContext.getResources().getColor(  
  117.                 R.color.grey_878787));  
  118.         btn_sort_recycle.setTextColor(mContext.getResources().getColor(  
  119.                 R.color.grey_878787));  
  120.   
  121.     }  

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

0 0