Custom layout for RadioButton

后端 未结 2 1012
既然无缘
既然无缘 2020-12-17 14:25

Is there any way I can change the layout for a RadioButton and still have the RadioGroup recognise it?

what I need is that the layout will include a couple of EditTe

相关标签:
2条回答
  • 2020-12-17 14:59

    I wrote a custom RadioGroup called RadioGroupPlus where it will traverse through it's children and find RadioButton regardless how deep the RadioButton is nested, it will then link all the RadioButton found together.

    You can find the repo here: https://github.com/worker8/RadioGroupPlus

    The README of the repo covers how to use it, and it actually works just like how you imagine it, for example:

    <worker8.com.github.radiogroupplus.RadioGroupPlus
        android:id="@+id/radio_group_plus"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout...>
           <ImageView...>
           <TextView...>
           <RadioButton...>
        </LinearLayout>
        <LinearLayout...>
           <ImageView...>
           <TextView...>
           <RadioButton...>
        </LinearLayout>
        <LinearLayout...>
           <ImageView...>
           <TextView...>
           <RadioButton...>
        </LinearLayout>
    </worker8.com.github.radiogroupplus.RadioGroupPlus>
    

    Will give you something like this:

    In your case, since you already have the xml layout file, try to download RadioGroupPlus by following the guide here:

    Add this to top level build.gradle:

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    

    Add this under dependencies:

    compile 'com.github.worker8:RadioGroupPlus:v1.0.1'
    

    Then in your xml, change RadioGroup to worker8.com.github.radiogroupplus.RadioGroupPlus. Now all your RadioButtons under RadioGroupPlus should all be linked together.

    Hope it helps!

    0 讨论(0)
  • 2020-12-17 15:05

    You'll have to create a class that extends RadioGroup, and override addView and PassThroughHierarchyChangeListener, to be able to use custom layouts for your radio button. By default, RadioGroup assumes that its children are radio buttons, see code below from the RadioGroup class:

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        if (child instanceof RadioButton) {
            final RadioButton button = (RadioButton) child;
            if (button.isChecked()) {
                mProtectFromCheckedChange = true;
                if (mCheckedId != -1) {
                    setCheckedStateForView(mCheckedId, false);
                }
                mProtectFromCheckedChange = false;
                setCheckedId(button.getId());
            }
        }
    
        super.addView(child, index, params);
    }
    
    private class PassThroughHierarchyChangeListener implements
            ViewGroup.OnHierarchyChangeListener {
        private ViewGroup.OnHierarchyChangeListener mOnHierarchyChangeListener;
    
        /**
         * {@inheritDoc}
         */
        public void onChildViewAdded(View parent, View child) {
            if (parent == RadioGroup.this && child instanceof RadioButton) {
                int id = child.getId();
                // generates an id if it's missing
                if (id == View.NO_ID) {
                    id = View.generateViewId();
                    child.setId(id);
                }
                ((RadioButton) child).setOnCheckedChangeWidgetListener(
                        mChildOnCheckedChangeListener);
            }
    
            if (mOnHierarchyChangeListener != null) {
                mOnHierarchyChangeListener.onChildViewAdded(parent, child);
            }
        }
    
        /**
         * {@inheritDoc}
         */
        public void onChildViewRemoved(View parent, View child) {
            if (parent == RadioGroup.this && child instanceof RadioButton) {
                ((RadioButton) child).setOnCheckedChangeWidgetListener(null);
            }
    
            if (mOnHierarchyChangeListener != null) {
                mOnHierarchyChangeListener.onChildViewRemoved(parent, child);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题