简体   繁体   English

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

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

I have a custom view that holds, among other views, a RadioButton. 我有一个自定义视图,其中包含一个RadioButton。

The SingleRadioItem: 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);
    }
}

I want to create instances of this view, add them to a RadioGroup and add the RadioGroup to a LinearLayout. 我想创建此视图的实例,将它们添加到RadioGroup并将RadioGroup添加到LinearLayout。 When I do so, it allows me to set all the radio buttons as selected, which means, the RadioGroup isn't functioning well (probably because how I do it..) 当我这样做时,它允许我将所有单选按钮设置为选中,这意味着,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);

Obviously, when I add RadioButton radio1 = new RadioButton(this); 显然,当我添加RadioButton radio1 = new RadioButton(this); the RadioGroup works well. RadioGroup效果很好。

Is it even possible to add a view that holds a radio button to a radiogroup and I'm just missing something or not possible at all? 是否甚至可以添加一个视图,将一个单选按钮添加到一个放射组,我只是遗漏了一些东西或根本不可能?

Thanks! 谢谢!

SOLUTION: To extend @cosmincalistru Answer and help others: 解决方案:扩展@cosmincalistru回答并帮助他人:

for each SingleRadioItem I added to the LinearLayout I attached a listener like this: 对于我添加到LinearLayout的每个SingleRadioItem我附加了一个这样的监听器:

radio1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

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

You also need to set the RadioButton View inside the SingleRadioItem XML to clickable:false. 您还需要将SingleRadioItem XML中的RadioButton视图设置为可单击:false。

The RadioButton has to be directly subordinated to the RadioGroup, otherwise your buttons will be considered as from different groups. RadioButton必须直接从属于RadioGroup,否则您的按钮将被视为来自不同的组。 The best idea is to use listeners on each RadioButton in your case. 最好的想法是在你的情况下在每个RadioButton上使用监听器。

EDIT: Whenever i want to make a group of RadioButtons as part from a group but can't use the RadioGroup i do something like this : 编辑:每当我想让一组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);
            }
        }
    });
}

When you add a view to RadioGroup, only if the view is a instanceof RadioButton only then will the group work correctly. 向RadioGroup添加视图时,仅当视图是RadioButton的实例时,该组才能正常工作。 In your case you are adding a LinearLayout. 在您的情况下,您正在添加LinearLayout。 So SingleRadioItem should extend RadioButton. 所以SingleRadioItem应该扩展RadioButton。

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

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