Android的:如何使AlertDialog与2线条与文字的单选按钮(单选)? [英] Android: How to make AlertDialog with 2 Text Lines and RadioButton (Single Choice)?

查看:214
本文介绍了Android的:如何使AlertDialog与2线条与文字的单选按钮(单选)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使一个列表对话框,像这样的行:

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?

推荐答案

我已经找到解决方案,<一个href=\"http://stackoverflow.com/questions/4842349/android-listview-with-radiobutton-checkbox-in-singlechoice-mode-and-a-custom-row\">here和这里

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.

所以我创建一个RelativeLayout的包装,用于扫描一个单选按钮,瞧,魔术完成。

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.

这篇关于Android的:如何使AlertDialog与2线条与文字的单选按钮(单选)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆