如何在alertdialog中单击确定按钮时知道单击了哪个单选按钮?

时间:2012-03-22 09:27:51

标签: android radio-button alertdialog

我有以下alertdialog代码:

    AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
    helpBuilder.setTitle("Options");
    helpBuilder.setMessage("Choose Your Option");

    LayoutInflater inflater = getLayoutInflater();
    View radioButtonLayout = inflater.inflate(R.layout.popuplayout, null);
    byNameRadioButton = (RadioButton) findViewById(R.id.byname);
    byIdRadioButton = (RadioButton) findViewById(R.id.byid);
    helpBuilder.setView(radioButtonLayout);
    helpBuilder.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                     Toast.makeText(Tab1Activity.this,
                     "Ok button is clicked", Toast.LENGTH_LONG)
                     .show();


                    }


                }
            });

    AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();

enter image description here

我想知道点击确定按钮时点击了哪个单选按钮? 需要帮助。 Thanx!

1 个答案:

答案 0 :(得分:3)

为什么不使用带有单选按钮的Dialog的默认实现,您可以在此处找到:Dialogs。您不需要编写任何额外的代码,您将获得所需的! :)  这是一个例子:

final CharSequence[] items = {"By Score", "By Name", "By Id"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Options");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();