簡體   English   中英

如何在Android的單選組內創建多行單選按鈕?

[英]How to create a multirow radio buttons inside a radio group in android?

嗨,我想在我的廣播組中創建一個多行單選按鈕。 如果我垂直使用收音機,我的設計會變得很奇怪,而且看起來也不好。 我有7個單選按鈕,我想連續顯示2個。 如果有人知道,請幫助我。

在您的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);
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM