学习笔记:
弹出对话框Dialog Builder的几种类型:
1.简单对话框
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("确认" ) ;
builder.setMessage("这是一个简单消息框" ) ;
builder.setPositiveButton("是" , null );
builder.show();
或
new AlertDialog.Builder(this)
.setTitle("标题" )
.setMessage("简单消息框" )
.setPositiveButton("确定" , null )
.setNegativeButton("否" , null)
.show();
都能起到简单对话框的作用。
2.输入文字
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("请输入");
builder.setIcon(R.drawable.ic_launcher);
builder.setView(new EditText(MainActivity.this));
builder.setPositiveButton("是" , null );
builder.setNegativeButton("否", null);
builder.show();
用setView方法,为我们的对话框传入了一个文本编辑框,还可以传入任何的视图对象,比如图片,WebView等
3.单选按钮
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("请选择");
builder.setIcon(R.drawable.ic_launcher);
builder.setSingleChoiceItems(new String[]{"选项1","选项2","选项3","选项4"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
builder.setNegativeButton("Cancel",null);
builder.show();
或
new AlertDialog.Builder(this)
.setTitle("请选择" )
.setIcon(android.R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[] {"选项1", "选项2", "选项3" , "选项4" }, 0 ,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
builder.setPositiveButton("sure",null); //交互不友好
.setNegativeButton("取消" , null )
.show();
4.多选框
AlertDialog.Builder
builder
=
new
Builder(MainActivity.this);
builder.setTitle("这是多选框");
builder.setIcon(R.drawable.ic_launcher);
builder.setMultiChoiceItems(new
String[]{"选项1",
"选项2",
"选项3",
"选项4"},
null,
null);
builder.setPositiveButton("sure",null);
builder.setNegativeButton("cancel",null);
builder.show();
AlertDialog.setView(view,0,0,0,0); //去除边框
或
new
AlertDialog.Builder(self)
.setTitle(
"多选框"
)
.setMultiChoiceItems(
new
String[] {
"选项1"
,
"选项2"
,
"选项3"
,
"选项4"
},
null
,
null
)
.setPositiveButton(
"确定"
,
null
)
.setNegativeButton(
"取消"
,
null
)
.show();
5.列表框
AlertDialog.Builder
builder
=
new
Builder(MainActivity.this);
builder.setTitle("提示框");
builder.setItems(new String[]{"选项1", "选项2", "选项3", "选项4"}, null);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消",null);
builder.show();
或
new
AlertDialog.Builder(self)
.setTitle(
"列表框"
)
.setItems(
new
String[] {
"列表项1"
,
"列表项2"
,
"列表项3"
},
null
)
.setNegativeButton(
"确定"
,
null
)
.show();
6.图片
ImageView img =
new
ImageView(self);
img.setImageResource(R.drawable.icon);
new
AlertDialog.Builder(self)
.setTitle(
"图片框"
)
.setView(img)
.setPositiveButton(
"确定"
,
null
)
.show();
可链外网图片
还有更多功能等待挖掘