将字符串数组作为数据源以列表方式显示在对话框中
投稿于 被查看 22580 次 评论:212
将字符串数组作为数据源以列表方式显示在对话框中
使用AlertDialog.Builder.setItems方法可以将一个String数组以列表形式显示在对话框中,代码:
String provinces[]= "辽宁省"," 山东省"," 河北省"," 福建省"," 广东省", "黑龙江省"," 浙江省","吉林省"
AlertDialog.Builder builder= AlertDialog.Builder( builder.setTitle("选择省份" builder.setItems(provinces, onClick(DialogInterface dialog, builder.setMessage("您选择了:"+which+":"+
builder.create().show();
效果如下图:
使用AlertDialog.Builder.setSingleChoiceItems方法不仅可以在对话框上显示列表,还可以在列表项后面显示选项按钮。代码:
String provinces[]= String[]{
"辽宁省"," 山东省"," 河北省"," 福建省"," 广东省", "黑龙江省"," 浙江省","吉林省"
AlertDialog.Builder builder= AlertDialog.Builder( builder.setTitle("选择省份" builder.setSingleChoiceItems(provinces, -1,
onClick(DialogInterface dialog, builder.setMessage("您已选择了:"+which+":"+
builder.setPositiveButton("关闭",
onClick(DialogInterface dialog,
builder.create().show();
效果图如下:
用户评论