Java 关闭AlertDialog后获取单选按钮状态

Java 关闭AlertDialog后获取单选按钮状态,java,android,radio-button,android-alertdialog,Java,Android,Radio Button,Android Alertdialog,我有一个活动,它用以下代码启动一个警报对话框。不幸的是,无论实际选中了哪个单选按钮,来自选中的RadioGroup的id始终是第一个单选按钮的id 我怀疑问题在于,当按下OK按钮时,视图已经被处理掉了,但我不知道如果是这样,为什么字符串仍然可以工作 为什么会发生这种情况,我可以做些什么来检查实际的单选按钮 AlertDialog.Builder builder = new AlertDialog.Builder(this); final View v = getLayoutInflater().

我有一个
活动
,它用以下代码启动一个
警报对话框
。不幸的是,无论实际选中了哪个单选按钮,来自选中的
RadioGroup
的id始终是第一个单选按钮的id

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

为什么会发生这种情况,我可以做些什么来检查实际的单选按钮

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>


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

你知道为什么我现在做的不管用吗?我不需要知道检查何时更改,只需要知道用户按OK时它处于什么位置。如果没有侦听器,您无法检测单选按钮是否已单击,因此您需要侦听器。在监听器的onCheckedChanged方法中,您可以检测所选的单选按钮。那么
getCheckedRadioButtonId()
方法用于什么?我以前没有使用过该方法,我进行了快速搜索。我想我找到了解决你问题的另一个办法。可能是关于getCheckedRadioButtonId()的问题的答案。希望这能有所帮助。你能编辑你的答案以反映最后的评论吗?