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



我如何在AlertDialog上列出一个列表,其中包括一些单选按钮,当我点击AlertDialog的项目时,做一些类似toast的事情。

试试这个:

//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();

如果这有帮助,请告诉我:(

最新更新