1.这是一个最简单的应用,就是弹出一个消息框,先看效果:
具体代码如下:
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("确认" ) ;
builder.setMessage("这是一个简单消息框" ) ;
builder.setPositiveButton("是" , null );
builder.show();
上面的代码中我们新建了一个AlertDialog,并用Builder方法形成了一个对象链,通过一系列的设置方法,构造出我们需要的对话框,然后调用 show方法显示出来,注意到Builder方法的参数 MainActivity.this,这个其实是Activity对象的引用,根据你所处的上下文来传入相应的引用就可以了。例如在onCreate方法中调用,只需传入this即 可。
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.setMessage("是否确认?" ) ;
builder.setPositiveButton("是",null );
builder.setNegativeButton("否", null);
builder.show();
这里为了方便,对两个按钮的监听响应事件都传入了null,但在具体处理中要分别写入具体的对应的处理代码。
4.单选按钮提示框,线上效果图
代码:
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();
builder.setTitle("确认" ) ;
builder.setMessage("是否确认?" ) ;
builder.setPositiveButton("是",null );
builder.setNegativeButton("否", null);
builder.show();
这里为了方便,对两个按钮的监听响应事件都传入了null,但在具体处理中要分别写入具体的对应的处理代码。
4.单选按钮提示框,线上效果图
代码:
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();
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();
builder.setItems(new String[]{"选项1", "选项2", "选项3", "选项4"}, null);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消",null);
builder.show();
6.多选框
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);
感谢:http://blog.sina.com.cn/s/blog_7f311ef50101ingb.html