【问题标题】:Getting radio button state after closing AlertDialog关闭 AlertDialog 后获取单选按钮状态
【发布时间】:2013-04-21 19:19:57
【问题描述】:

我有一个Activity,它使用以下代码启动一个AlertDialog。不幸的是,来自RadioGroup 的 id 被检查是总是第一个单选按钮的 ID,无论实际检查了哪个单选按钮。

我怀疑问题是当按下 OK 按钮时视图已经被处理掉了,但我不知道为什么在这种情况下字符串仍然可以工作。

为什么会发生这种情况,我该怎么做才能检查 actual 单选按钮?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
final View v = getLayoutInflater().inflate(R.layout.dialog_add_team, null);
builder.setView(v);
// Add the buttons
builder.setPositiveButton(R.string.ok,
    new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
            String name = ((EditText) v.findViewById(R.id.team_name)).getText()+"";
            if (name.equals(""))
                return;
            int checkedId=((RadioGroup)v.findViewById(R.id.radioGroup1))
                              .getCheckedRadioButtonId();
            teamAdded(name, checkedId);
        }
    });
    builder.setNegativeButton(R.string.cancel,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });

    AlertDialog dialog = builder.create();
    dialog.show();

}

这是我的布局的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/team_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/teamname" >

        <requestFocus />
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/bothgenders" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/boys" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/girls" />
    </RadioGroup>

</LinearLayout>

【问题讨论】:

    标签: java android radio-button android-alertdialog


    【解决方案1】:

    我认为您应该将 setOnCheckedChangeListener 设置为您的 radioGroup。您可以使用this 回答。希望这会有所帮助。

    【讨论】:

    • 你知道为什么我正在做的事情不起作用吗?我不需要知道检查何时发生变化,只需要知道用户按下 OK 时它在什么位置。
    • 如果没有监听器,您将无法检测到单选按钮是否被单击,因此您需要一个监听器。在侦听器的 onCheckedChanged 方法中,您可以检测到选定的单选按钮。
    • 那么getCheckedRadioButtonId()方法是干什么用的?
    • 我之前没有使用过这种方法,我快速搜索了一下。我想我找到了解决您问题的替代方法。 stackoverflow.com/a/6441097/909497 可能是您关于 getCheckedRadioButtonId() 问题的答案。希望这会有所帮助。
    • 您能否编辑您的答案以反映最后一条有效的评论?
    猜你喜欢
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多