Как узнать, какая кнопка переключателя была нажата при нажатии кнопки ok в модуле алертилдиалог? - PullRequest
1 голос
/ 22 марта 2012

У меня есть следующий код для alerttdialog:

    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

Я хочу знать, какая кнопка была нажата при нажатии кнопки OK?Нужна помощь.Спасибо!

1 Ответ

3 голосов
/ 22 марта 2012

Почему бы вам не использовать стандартную реализацию 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();
...