【问题标题】:How to create a multirow radio buttons inside a radio group in android?如何在android中的单选组内创建多行单选按钮?
【发布时间】:2014-11-10 21:02:56
【问题描述】:

您好,我想在我的单选组中创建一个多行单选按钮。如果我垂直使用收音机组,我的设计会变得很奇怪,而且看起来不太好。我有 7 个单选按钮,我想连续显示 2 个。如果有人知道,请帮助我。

【问题讨论】:

  • 你有任何设计,如果有的话
  • 无法发布图片。我是新来的,只有一个声誉。
  • 图片发布不会影响声誉,因此可以尽快创建设计和发布图片,我们可以在 android 中创建任何类型的布局
  • 只需在某处发布屏幕截图并在此处放置链接。你的意思是你想要单选按钮,每个单选按钮都有一个垂直行?你不是说 android:orientation 吗?

标签: android radio-button


【解决方案1】:

添加你的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:custom="http://schemas.android.com/apk/res/com.example.app"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<com.example.app.GroupedRadioButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Option 1"
    custom:group="group1" />

<com.example.app.GroupedRadioButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Option 2"
    custom:group="group1" />

<com.example.app.GroupedRadioButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Option 3"
    custom:group="group1" />

<com.example.app.GroupedRadioButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Option A"
    custom:group="group2" />

<com.example.app.GroupedRadioButton
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Option B"
    custom:group="group2" />

</LinearLayout>

// 在 attr.xml 文件中添加这一行

<declare-styleable name="GroupedRadioButton">
<attr name="group" format="string" />
</declare-styleable>



public class RadioGroupedButton extends RadioButton {
        private static Map<String, WeakReference<RadioGroupedButton>> buttonMap;
        private OnClickListener externalListener;
        private String groupName;

        static {
            buttonMap = new HashMap<String, WeakReference<RadioGroupedButton>>();
        }

        public RadioGroupedButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            processAttributes(context, attrs);
            setOnClickListener(internalListener, true);
        }

        private void processAttributes(Context context, AttributeSet attrs) {
            TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.GroupedRadioButton);
            int attributeCount = attributes.getIndexCount();
            for (int i = 0; i < attributeCount; ++i) {
                int attr = attributes.getIndex(i);
                switch (attr) {
                case R.styleable.GroupedRadioButton_group:
                    this.groupName = attributes.getString(attr);
                    break;
                }
            }
            attributes.recycle();
        }

        private OnClickListener internalListener = new OnClickListener() {

            @Override
            public void onClick(View view) {
                processButtonClick(view);
            }
        };

        private void setOnClickListener(OnClickListener listener, boolean internal) {
            if (internal)
                super.setOnClickListener(internalListener);
            else
                this.externalListener = listener;
        }

        @Override
        public void setOnClickListener(OnClickListener listener) {
            setOnClickListener(listener, false);
        }

        private void processButtonClick(View view) {
            if (!(view instanceof RadioGroupedButton))
                return;

            RadioGroupedButton clickedButton = (RadioGroupedButton) view;
            String groupName = clickedButton.groupName;
            WeakReference<RadioGroupedButton> selectedButtonReference = buttonMap.get(groupName);
            RadioGroupedButton selectedButton = selectedButtonReference == null ? null
                    : selectedButtonReference.get();
            if (selectedButton != clickedButton) {
                if (selectedButton != null)
                    selectedButton.setChecked(false);
                clickedButton.setChecked(true);
                buttonMap.put(groupName, new WeakReference<RadioGroupedButton>(clickedButton));
            }
            if (externalListener != null)
                externalListener.onClick(view);
        }

    }

【讨论】:

    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 2016-02-10
    • 2013-06-19
    • 1970-01-01
    相关资源
    最近更新 更多