创建一个包含列表的警报对话框

时间:2021-05-29 13:03:08

标签: java list radio-button android-alertdialog

如何在 AlertDialog 上创建一个列表,其中包含一些单选按钮,当我点击 AlertDialog 的项目时,执行类似 toast 的操作。

1 个答案:

答案 0 :(得分:0)

试试这个:

//Setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose an animal");// add a radio button list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
int checkedItem = 1; // cow
builder.setSingleChoiceItems(animals, checkedItem, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Here you can implement Toast Message
    }
});
//Add OK and Cancel buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        //Handle when user clicks OK
    }
});
builder.setNegativeButton("Cancel", null); //Create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();

让我知道这是否有帮助:)

相关问题