Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将带有单选按钮的自定义视图添加到单选组_Android_Radio Button_Radio Group - Fatal编程技术网

Android 将带有单选按钮的自定义视图添加到单选组

Android 将带有单选按钮的自定义视图添加到单选组,android,radio-button,radio-group,Android,Radio Button,Radio Group,我有一个自定义视图,其中包含一个单选按钮 单一项目: public class SingleRadioItem extends LinearLayout { private TextView mTextKey; private RadioButton mRadioButton; private ImageView mImageSeparator; public SingleRadioItem(Context context, AttributeSet attrs

我有一个自定义视图,其中包含一个单选按钮

单一项目:

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);
    }
}
我要创建此视图的实例,将它们添加到一个放射组,并将放射组添加到一个LinearLayout。 当我这样做时,它允许我将所有单选按钮设置为选中状态,这意味着RadioGroup运行不正常(可能是因为我的操作方式…)

显然,当我添加
RadioButton radio1=新的RadioButton(这个)RadioGroup运行良好

是否有可能添加一个视图,其中包含一个单选按钮到一个放射组,而我只是缺少了一些东西或根本不可能

谢谢

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

对于我添加到LinearLayout的每个单独的放射性项目,我附加了一个侦听器,如下所示:

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视图设置为clickable:false。

将视图添加到RadioGroup时,只有当视图是RadioButton的实例时,组才能正常工作。在本例中,您正在添加线性布局。所以我们应该扩展RadioButton

RadioButton必须直接从属于RadioGroup,否则您的按钮将被视为来自不同的组。 最好的办法是在您的案例中的每个单选按钮上使用侦听器

编辑: 每当我想从一个组中创建一组单选按钮,但不能使用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);
            }
        }
    });
}
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);
            }
        }
    });
}