繁体   English   中英

使用单选按钮向自定义组添加自定义视图

[英]Adding custom view with radio button to radio group

我有一个自定义视图,其中包含一个RadioButton。

SingleRadioItem:

public class SingleRadioItem extends LinearLayout {
    private TextView mTextKey;
    private RadioButton mRadioButton;
    private ImageView mImageSeparator;

    public SingleRadioItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.rtl_single_radio_item, this, true);

        mTextKey = (TextView)view.findViewById(R.id.single_radio_item_text_key);
        mRadioButton = (RadioButton)view.findViewById(R.id.single_radio_item_button);
        mImageSeparator = (ImageView)view.findViewById(R.id.single_image_separator);
    }

    public void setKey(String key) {
        mTextKey.setText(key);
    }

    public boolean getSelectedState() {
        return mRadioButton.isSelected();
    }

    public void setSelectedState(boolean selected) {
        mRadioButton.setSelected(selected);
    }
}

我想创建此视图的实例,将它们添加到RadioGroup并将RadioGroup添加到LinearLayout。 当我这样做时,它允许我将所有单选按钮设置为选中,这意味着,RadioGroup运行不正常(可能是因为我这样做...)

RadioGroup radioGroup = new RadioGroup(this);
        radioGroup.setOrientation(RadioGroup.VERTICAL);

        SingleRadioItem radio1 = new SingleRadioItem(this, null);
        SingleRadioItem radio2 = new SingleRadioItem(this, null);

        radioGroup.addView(radio1);
        radioGroup.addView(radio2);

        updateDetailsView.addView(radioGroup);

显然,当我添加RadioButton radio1 = new RadioButton(this); RadioGroup效果很好。

是否甚至可以添加一个视图,将一个单选按钮添加到一个放射组,我只是遗漏了一些东西或根本不可能?

谢谢!

解决方案:扩展@cosmincalistru回答并帮助他人:

对于我添加到LinearLayout的每个SingleRadioItem我附加了一个这样的监听器:

radio1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (lastRadioChecked != null) {
                    lastRadioChecked.setCheckedState(false);
                }

                lastRadioChecked = (SingleRadioItem)v;
                lastRadioChecked.setCheckedState(true);
            }
        });

您还需要将SingleRadioItem XML中的RadioButton视图设置为可单击:false。

RadioButton必须直接从属于RadioGroup,否则您的按钮将被视为来自不同的组。 最好的想法是在你的情况下在每个RadioButton上使用监听器。

编辑:每当我想让一组RadioButtons作为一个组的一部分,但不能使用RadioGroup我做这样的事情:

RadioButton r1,r2,....;
// Instantiate all your buttons;
...
// Set listener on each
for(each RadioButton) {
    rx.setOnCheckedChangeListener(OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                //set all buttons to false;
                for(each RadioButton) {
                    rx.setChecked(false);
                }
                //set new selected button to true;
                buttonView.setChecked(true);
            }
        }
    });
}

向RadioGroup添加视图时,仅当视图是RadioButton的实例时,该组才能正常工作。 在您的情况下,您正在添加LinearLayout。 所以SingleRadioItem应该扩展RadioButton。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM