设计一个AlertDialog的类,弹出框用setview,根据传递过来的String[],设置单选,确认后返回选择结果,请给出代码
时间: 2024-05-07 10:16:32 浏览: 43
Android编程实现AlertDialog自定义弹出对话框的方法示例
好的,这是一个关于AlertDialog的代码示例:
```
public class MyAlertDialog {
private Context context;
private String[] items;
private int choice = -1;
public MyAlertDialog(Context context, String[] items) {
this.context = context;
this.items = items;
}
public int createAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("请选择你的答案:");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
choice = which;
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something when click "OK"
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something when click "Cancel"
}
});
builder.setView(view);
AlertDialog dialog = builder.create();
dialog.show();
return choice;
}
}
```
这个类中,我们接收一个字符串数组作为选择项,并且使用setSingleChoiceItems方法将其设置为单选项。当用户点击“确定”按钮时,我们将选择结果返回。
阅读全文