【发布时间】:2021-08-17 11:01:54
【问题描述】:
如何在 AlertDialog 上创建一个列表,其中包含一些单选按钮,当我单击 AlertDialog 的项目时,执行类似 toast 的操作。
【问题讨论】:
标签: java list radio-button android-alertdialog
如何在 AlertDialog 上创建一个列表,其中包含一些单选按钮,当我单击 AlertDialog 的项目时,执行类似 toast 的操作。
【问题讨论】:
标签: java list radio-button android-alertdialog
试试这个:
//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();
如果有帮助请告诉我:)
【讨论】: