Android: How to make AlertDialog with 2 Text Lines and RadioButton (Single Choice)?

六月ゝ 毕业季﹏ 提交于 2019-12-19 04:20:08

问题


How to make a List Dialog with rows like this:

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

I know I should use a custom adapter, passing a row layout with those views (actually, I've made this). But the RadioButton does not get selected when I click on the row.

Is it possible that the dialog manage the radiobuttons it self?


回答1:


I've found solutions here and here.

Basically, we have to create a "Checkable" layout, because the view's root item must implement the Checkable interface.

So I create a RelativeLayout wrapper that scans for a RadioButton and voilá, the magic is done.

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();
        }
    }

}

You can do it with Checkbox or whatever you need.



来源:https://stackoverflow.com/questions/5346406/android-how-to-make-alertdialog-with-2-text-lines-and-radiobutton-single-choic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!