zoukankan      html  css  js  c++  java
  • android 多行 RadioButton的使用

    最近项目用到了多行RadioButton,随记录下.

    先给出RadioButton的布局

    <com.kuibu.jucai.widget.MyRadioGroup
    android:id="@+id/myRadioGroup"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
    android:id="@+id/rb_50"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_marginLeft="@dimen/space_10"
    android:layout_weight="1"
    android:background="@drawable/poundage_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="@dimen/space_10"
    android:paddingLeft="@dimen/space_20"
    android:paddingRight="@dimen/space_20"
    android:paddingTop="@dimen/space_10"
    android:text="50¥"
    android:textColor="@drawable/fg_order_text_selector" />


    <RadioButton
    android:id="@+id/rb_100"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_marginLeft="@dimen/space_10"
    android:layout_weight="1"
    android:background="@drawable/poundage_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="@dimen/space_10"
    android:paddingLeft="@dimen/space_20"
    android:paddingRight="@dimen/space_20"
    android:paddingTop="@dimen/space_10"
    android:text="100¥"
    android:textColor="@drawable/fg_order_text_selector" />

    <RadioButton
    android:id="@+id/rb_150"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_marginLeft="@dimen/space_10"
    android:layout_marginRight="@dimen/space_10"
    android:layout_weight="1"
    android:background="@drawable/poundage_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="@dimen/space_10"
    android:paddingLeft="@dimen/space_30"
    android:paddingRight="@dimen/space_30"
    android:paddingTop="@dimen/space_10"
    android:text="150¥"
    android:textColor="@drawable/fg_order_text_selector" />

    </LinearLayout>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioButton
    android:id="@+id/rb_200"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_marginLeft="@dimen/space_10"
    android:layout_marginTop="@dimen/space_10"
    android:layout_weight="1"
    android:background="@drawable/poundage_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="@dimen/space_10"
    android:paddingLeft="@dimen/space_20"
    android:paddingRight="@dimen/space_20"
    android:paddingTop="@dimen/space_10"
    android:text="200¥"
    android:textColor="@drawable/fg_order_text_selector" />

    <RadioButton
    android:id="@+id/rb_500"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_marginLeft="@dimen/space_10"
    android:layout_marginTop="@dimen/space_10"
    android:layout_toRightOf="@+id/rb_200"
    android:layout_weight="1"
    android:background="@drawable/poundage_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="@dimen/space_10"
    android:paddingLeft="@dimen/space_20"
    android:paddingRight="@dimen/space_20"
    android:paddingTop="@dimen/space_10"
    android:text="500¥"
    android:textColor="@drawable/fg_order_text_selector" />

    <RadioButton
    android:id="@+id/rb_1000"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:layout_marginLeft="@dimen/space_10"
    android:layout_marginRight="@dimen/space_10"
    android:layout_marginTop="@dimen/space_10"
    android:layout_toRightOf="@+id/rb_500"
    android:layout_weight="1"
    android:background="@drawable/poundage_selector"
    android:button="@null"
    android:gravity="center"
    android:paddingBottom="@dimen/space_10"
    android:paddingLeft="@dimen/space_20"
    android:paddingRight="@dimen/space_20"
    android:paddingTop="@dimen/space_10"
    android:text="1000¥"
    android:textColor="@drawable/fg_order_text_selector" />
    </LinearLayout>
    </com.kuibu.jucai.widget.MyRadioGroup>

    这里用到了一个自定义控件,如下
    public class MyRadioGroup extends RadioGroup {

    //对外暴漏
    private OnCheckedChangeListener mOnCheckedChangeListener;

    public MyRadioGroup(Context context) {
    super(context);
    }

    public MyRadioGroup(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
    mOnCheckedChangeListener = listener;
    }

    @Override
    public void addView(final View child, int index, ViewGroup.LayoutParams params) {
    if (child instanceof LinearLayout) {
    int childCount = ((LinearLayout) child).getChildCount();
    for (int i = 0; i < childCount; i++) {
    View view = ((LinearLayout) child).getChildAt(i);
    if (view instanceof RadioButton) {
    final RadioButton button = (RadioButton) view;
    ((RadioButton) button).setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
    ((RadioButton) button).setChecked(true);
    checkRadioButton((RadioButton) button);
    if (mOnCheckedChangeListener != null) {
    mOnCheckedChangeListener.onCheckedChanged(MyRadioGroup.this, button.getId());
    }
    return true;
    }
    });
    }
    }
    }

    super.addView(child, index, params);
    }

    private void checkRadioButton(RadioButton radioButton) {
    View child;
    int radioCount = getChildCount();
    for (int i = 0; i < radioCount; i++) {
    child = getChildAt(i);
    if (child instanceof RadioButton) {
    if (child == radioButton) {
    // do nothing
    } else {
    ((RadioButton) child).setChecked(false);
    }
    } else if (child instanceof LinearLayout) {
    int childCount = ((LinearLayout) child).getChildCount();
    for (int j = 0; j < childCount; j++) {
    View view = ((LinearLayout) child).getChildAt(j);
    if (view instanceof RadioButton) {
    final RadioButton button = (RadioButton) view;
    if (button == radioButton) {
    // do nothing
    } else {
    ((RadioButton) button).setChecked(false);
    }
    }
    }
    }
    }
    }
    }

    网络不好,无法直接插入代码,只能这样拷贝了.


  • 相关阅读:
    Linux文件和目录的权限
    nginx的默认web目录
    SSH-keygen rsa 密钥对根据私钥生成公钥
    对ajax的理解
    开关式复选框的操作
    关于弹框和原页面的切换问题
    单选框和复选框中value值得获取
    php中明明写了类函数,却报致命错误找不到类
    python函数
    python关键字
  • 原文地址:https://www.cnblogs.com/android-zcq/p/5871164.html
Copyright © 2011-2022 走看看