带有单选按钮和验证按钮的对话框

14

enter image description here
我想在第二个单选按钮 B 下面居中添加一个按钮,并且当我选择一个选项并单击验证时,会发生某些操作。有什么帮助吗?

final CharSequence[] photo = {"A","B"};

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Select Gender");

alert.setSingleChoiceItems(photo,-1, new 

DialogInterface.OnClickListener()

{

    @Override
    public void onClick(DialogInterface dialog, int which) 
    {
        if(photo[which]=="A")

        {

            gen="B";
        }

        else if (photo[which]=="B")

        {

            gen="B";

        }
    }

});
alert.show();
4个回答

17

尝试这样做,你只需要选择默认选项并将一个整数添加到对话框中 -> inputSelection

final CharSequence[] items = { " HDMI IN ", " AV IN" };

        // Creating and Building the Dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Select Input Type");

        builder.setSingleChoiceItems(items,inputSelection,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        inputSelection = item;
                        levelDialog.dismiss();
                    }
                });
        levelDialog = builder.create();
        levelDialog.show();

13
我的方法制作自定义对话框
参考这里的http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application 1. 创建一个 XML 自定义对话框
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/rd_!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A" />

    <RadioButton
        android:id="@+id/rd_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd_!"
        android:text="B" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rd_2"
        android:layout_centerInParent="true"
        android:text="OK" />
    </RelativeLayout>

以及activity.java文件。

  Dialog dialog = new Dialog(Dialogeshow.this);
    dialog.setContentView(R.layout.custom_dialoge);
    dialog.setTitle("This is my custom dialog box");
    dialog.setCancelable(true);
    // there are a lot of settings, for dialog, check them all out!
    // set up radiobutton
    RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_);
    RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2);

    // now that the dialog is set up, it's time to show it
    dialog.show();

你应该在xml文件中将你的RadioButton添加到RadioGroup。 - kostyabakay
如何添加onclicklistener - Harish Reddy
1
@HarishReddy 给听众们:RadioButton rd1 =(RadioButton)dialog.findViewById(R.id.rd_1); rd1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ } }); - David Ortiz

5
您可以使用以下方法显示对话框。
public void showDialog(Context context, String title, String[] btnText,
        DialogInterface.OnClickListener listener) {

    final CharSequence[] items = { "One", "Two" };

    if (listener == null)
        listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface paramDialogInterface,
                    int paramInt) {
                paramDialogInterface.dismiss();
            }
        };
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(title);

    builder.setSingleChoiceItems(items, -1,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {

                }
            });
    builder.setPositiveButton(btnText[0], listener);
    if (btnText.length != 1) {
        builder.setNegativeButton(btnText[1], listener);
    }
    builder.show();
}

以下是调用部分的示例:

showDialog(MainActivity.this, "Your Title", new String[] { "Ok" },
    new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            if(which==-1)
            Log.d("Neha", "On button click");
            //Do your functionality here
        }
    });

1
我想知道如何使用您的解决方案检查单选按钮是否被选中。 - Dimitri
3
当我称呼它时,两者的值都是-1。 - Dimitri
你能解释一下在这段代码中选择项目时圆圈如何变为绿色吗??我们可以直接在某个视图的onClick监听器中调用此警示对话框而不使用showDialog方法吗??我尝试过了,但是圆圈没有显示选中。@Neha Dhanwani - user3623979
1
你可以添加以下代码行:int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition(); - Kamil Nękanowicz
解决方案目前无法正常工作,始终为 which=-1。您测试过这段代码并获得了正确的选定项吗? - MBH

1
您可以使用 Builder.setNeutralButton 向您的对话框中添加单个按钮。

但是如何只在按钮点击时选择单选框选项 - Dimitri
请参考此网站:http://wptrafficanalyzer.in/blog/alert-dialog-window-with-radio-buttons-in-android/ - AndroidEnthusiastic
@Peter,你的解决方案不支持Froyo 2.2。 - Dimitri
@sophia 在 AndroidManifest 文件中将 minsdk 设置为 8 并运行你的代码 :) - AndroidEnthusiastic
<uses-sdk android:minSdkVersion="8" /> - Dimitri
@sophia 在那个URL中,他们使用了片段概念。 片段支持版本3+。 所以请尝试其他选项http://android-codes-examples.blogspot.in/2011/03/how-to-display-alertdialog-and.html - AndroidEnthusiastic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接