Android:如何使用2个文本行和RadioButton(单选)制作AlertDialog?

时间:2011-03-17 23:11:27

标签: android android-layout alertdialog

如何使用如下行创建列表对话框:

|-----------------------------|
| FIRST LINE OF TEXT      (o) | <- this is a "RadioButton"
| second line of text         |
|-----------------------------|

我知道我应该使用自定义适配器,使用这些视图传递行布局(实际上,我已经制作了这个)。但是当我点击该行时,RadioButton不会被选中。

对话框是否可能自行管理单选按钮?

1 个答案:

答案 0 :(得分:2)

我找到了解决方案herehere

基本上,我们必须创建一个“可检查”布局,因为视图的根项必须实现Checkable接口。

所以我创建了一个RelativeLayout包装器来扫描RadioButton和voilá,魔术就完成了。

public class CheckableLayout extends RelativeLayout implements Checkable
{
    private RadioButton _checkbox;

    public CheckableLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate()
    {
        super.onFinishInflate();
        // find checkable view
        int childCount = getChildCount();
        for (int i = 0; i < childCount; ++i)
        {
            View v = getChildAt(i);
            if (v instanceof RadioButton)
            {
                _checkbox = (RadioButton) v;
            }
        }
    }

    public boolean isChecked()
    {
        return _checkbox != null ? _checkbox.isChecked() : false;
    }

    public void setChecked(boolean checked)
    {
        if (_checkbox != null)
        {
            _checkbox.setChecked(checked);
        }
    }

    public void toggle()
    {
        if (_checkbox != null)
        {
            _checkbox.toggle();
        }
    }

}

你可以使用Checkbox或任何你需要的东西。