高仿快遞100--實戰之RadioGroup和RadioButton應用,100--radiogroup
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() {@Overridepublic 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的應用也不少,大同小異,關於這個小知識點就介紹到這了。