请问一下关于Java编程的一下小疑惑

img


如图中蓝色部分所示,这段程序中,我本意是为了让文本框中的内容以及选中的RadioButton的内容都输入到文本域,但是现在这段蓝色代码,只能实现文本框中的内容输入文本域或将选中的RadioButton输入文本域?但是无法将两者的内容都同时输入文本域。

百度

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这篇博客: RadioButton的排版,图标样式修改和图标文字间距修改中的 2.使用其他的布局控件排版RadioButton 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    LinearLayout
    RelativeLayout
    ConstraintLayout
    //其他的等等
    

    使用如ConstraintLayout等布局排版,也就意味着失去RadioGroup的单选互斥功能;需要自己来实现

    单选RadioButton互斥功能实现工具类

    //单选RadioButton互斥功能工具类
    public class RadioButtonSelectOnlyOneHelper {
        private ArrayList<RadioButton> radioButtons;
        private CompoundButton.OnCheckedChangeListener mChildOnCheckedChangeListener;
        // when true, mOnCheckedChangeListener discards events
        private boolean mProtectFromCheckedChange = false;
        private OnCheckedChangeListener mOnCheckedChangeListener;
        private int checkedIndex = -1;
    
        public RadioButtonSelectOnlyOneHelper() {
            radioButtons = new ArrayList<>();
            mChildOnCheckedChangeListener = new CheckedStateTracker();
        }
        public void setOnCheckedChangeListener(RadioButtonSelectOnlyOneHelper.OnCheckedChangeListener listener) {
            mOnCheckedChangeListener = listener;
        }
        /**
         * 添加单个RadioButton到分组里
         *
         * @param item
         */
        public void addRadioButton(RadioButton item) {
            if (item == null) return;
            if (item.isChecked()) {
                //处理有多个RadioButton的默认值为选中状态,只保留第一个
                mProtectFromCheckedChange = true;
                if (checkedIndex == -1) {
                    checkedIndex = radioButtons.size();
                } else {
                    item.setChecked(false);
                }
                mProtectFromCheckedChange = false;
            }
            item.setOnCheckedChangeListener(mChildOnCheckedChangeListener);
            radioButtons.add(item);
        }
        private void addView(View item) {
            if (item == null) return;
            if (item instanceof RadioButton) {
                addRadioButton((RadioButton) item);
            } else if (item instanceof ViewGroup) {
                addRadioButtonParent((ViewGroup) item);
            }
        }
        /**
         * 添加的ViewGroup中的RadioButton到分组里
         *
         * @param radioButtonParent
         */
        public void addRadioButtonParent(ViewGroup radioButtonParent) {
            if (radioButtonParent == null) return;
            int childCount = radioButtonParent.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = radioButtonParent.getChildAt(i);
                addView(view);
            }
        }
        /**
         * 设置文本是checkedText的RadioButton为选中状态
         *
         * @param checkedText
         */
        public void setCheck(String checkedText) {
            Iterator<RadioButton> eIterator = radioButtons.iterator();
            while (eIterator.hasNext()) {
                RadioButton radioButton = eIterator.next();
                if (TextUtils.equals(radioButton.getText().toString(), checkedText)) {
                    setCheckedRadioButton(radioButton);
                    break;
                }
            }
        }
        /**
         * 获取选中的RadioButton,没有选中的就返回null
         *
         * @return
         */
        public RadioButton getCheckedRadioButton() {
            if (checkedIndex != -1 && checkedIndex < radioButtons.size()) {
                return radioButtons.get(checkedIndex);
            }
            return null;
        }
        private void setCheckedRadioButton(RadioButton checkedRadioButton) {
            mProtectFromCheckedChange = true;
            if (checkedIndex != -1) {
                radioButtons.get(checkedIndex).setChecked(false);
            }
            checkedIndex = radioButtons.indexOf(checkedRadioButton);
            checkedRadioButton.setChecked(true);
            mProtectFromCheckedChange = false;
        }
        /**
         * <p>Interface definition for a callback to be invoked when the checked
         * radio button changed in this group.</p>
         */
        public interface OnCheckedChangeListener {
            public void onCheckedChanged(RadioButton checkedRadioButton);
        }
    
        private class CheckedStateTracker implements CompoundButton.OnCheckedChangeListener {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mProtectFromCheckedChange || !buttonView.isChecked()) {
                    return;
                }
                setCheckedRadioButton((RadioButton) buttonView);
                if (mOnCheckedChangeListener != null) {
                    mOnCheckedChangeListener.onCheckedChanged((RadioButton) buttonView);
                }
            }
        }
    }
    
    

    工具类使用

            View view = inflater.inflate(R.layout.my_layout, null);
            RadioButtonSelectOnlyOneHelper radioButtonSelectOnlyOneHelper = new RadioButtonSelectOnlyOneHelper();
            radioButtonSelectOnlyOneHelper.addRadioButtonParent(view.findViewById(R.id.radio_1));
            radioButtonSelectOnlyOneHelper.addRadioButtonParent(view.findViewById(R.id.radio_2));
            radioButtonSelectOnlyOneHelper.setOnCheckedChangeListener(new RadioButtonSelectOnlyOneHelper.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioButton checkedRadioButton) {
                    LogUtil.e(TAG,"onCheckedChanged:"+checkedRadioButton.getText().toString());
                }
            });
    
    

    radio_1和radio_2是RadioButton 的父布局控件


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^