android 多行 RadioButton的使用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}
}
}
}
}
}
}
网络不好,无法直接插入代码,只能这样拷贝了.
以上是关于android 多行 RadioButton的使用的主要内容,如果未能解决你的问题,请参考以下文章
Android RadioGroup和RadioButton详解
Android从零单排系列十《Android视图控件——RadioButton》
Android从零单排系列十《Android视图控件——RadioButton》
Android从零单排系列十《Android视图控件——RadioButton》