AlertDialog是用来和用户交流互动的很好的工具,善用之可以为应用程序增色。有人认为它简单”不就一个对话框么“,我觉得技术是需要严谨甚至谦卑。手机屏幕是个寸土必争之地,那么既然点进来看此文了,说明还是对AlertDialog想了解更多的好学人士。此文的目标:不再搜索”Android AlertDialog“!

先来看一个最简单的AlertDialog:

Android AlertDialog的一切_第1张图片

其实,我觉得这个最基本的AlertDialog已经足够好看的了。下面是实现代码:

/** AlertDialog.Builder 是用来创建AlertDialog的 */AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder//给builder set各种属性值    .setIcon(R.drawable.blink)//继续set    .setMessage(getString(R.string.alert_dialog_message))    .setPositiveButton("确定退出", new OnClickListener() {//确定按钮        @Override        public void onClick(DialogInterface dialog, int which) {            MainActivity.this.finish();            System.exit(0);        }    })    .setNegativeButton("我按错了", new OnClickListener() {//取消按钮        @Override        public void onClick(DialogInterface dialog, int which) {            dialog.dismiss();        }    })    .show();//显示AlertDialog

这里,也许会奇怪,为什么没有直接见到AlertDialog呢?而是用了一个Builder,set了一些值之后直接.show()就出来了?

如果有这么浓厚的好奇心,还是要看一下AlertDialog的源码:

public class AlertDialog extends Dialog implements DialogInterface

首先AlertDialog继承自Dialog实现了DialogInterface接口,那么使用的时候也可以考虑用一下Dialog的函数。

protected AlertDialog(Context context)

构造方法都是用protected来修饰的,说明我们没有办法直接new AlertDialog(),Google给我们提供了一个AlertDialog的内部类AlertDialog.Builder来实现:

public static class Builder

很欣喜的看到public修饰符了,这也就是上文使用AlertDialog.Builder的原因。


对于AlertDialog.Builder的理解,从字面上看出,它是用来构建AlertDialog的,可以概括一下,它是为AlertDialog做一些准备工作的。下面我们来看看AlertDialog对象的使用,还是先看效果,这才有兴趣往下看呐:

Android AlertDialog的一切_第2张图片

代码只是略加改动,体现了AlertDialog对象的作用:

/** AlertDialog.Builder 是用来创建AlertDialog的 */AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);AlertDialog alertDialog =        builder//给builder set各种属性值            .setIcon(R.drawable.blink)//继续set            .setTitle(getString(R.string.alert_dialog_message))            .setMessage(getString(R.string.alert_dialog_message))            .setPositiveButton("确定退出", new OnClickListener() {//确定按钮                @Override                public void onClick(DialogInterface dialog, int which) {                    MainActivity.this.finish();                    System.exit(0);                }            })            .setNegativeButton("我按错了", new OnClickListener() {//取消按钮                @Override                public void onClick(DialogInterface dialog, int which) {                    dialog.dismiss();                }            })            .create();//创建AlertDialog对象alertDialog.setMessage("AlertDialog对象:\n\t\t" + getString(R.string.alert_dialog_message));alertDialog.show();


自定义AlertDialog,我觉得效果还不如默认的好:

Android AlertDialog的一切_第3张图片

布局文件alert_dialog_custom.xml

<?xml version="1.0" encoding="utf-8"?>        

使用自定义布局很简单:

builder.setView(LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_dialog_custom, null));

AlertDialog.Builder提供了setView的方法来使用自定义布局。


AlertDialog.Builder的setView方法是在AlertDialog的Message下面提供了一个自定义布局的空间,并不能改变整个AlertDialog的风格。下面请看改变整体风格的AlertDialog:

Android AlertDialog的一切_第4张图片

布局文件可以自己任意发挥,主要还是看:

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();//Builder直接create成AlertDialogalertDialog.show();//AlertDialog先得show出来,才能得到其WindowWindow window = alertDialog.getWindow();//得到AlertDialog的Windowwindow.setContentView(R.layout.alert_dialog_custom);//给Window设置自定义布局View layout = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_dialog_custom, null);//从xml中inflate过来TextView dialogMsg = (TextView) window.findViewById(R.id.alert_dialog_message);//从Window中findViewdialogMsg.setOnClickListener(new View.OnClickListener() {//设置监听    @Override    public void onClick(View v) {        MainActivity.this.finish();        System.exit(0);    }});


关于AlertDialog,我想到的就这么多了,抛砖引玉。


更多相关文章

  1. 在Android中用纯Java代码布局
  2. Android设备预计将超微软
  3. Android帧布局
  4. Android入门篇四:使用剪切板在Activity之间传递对象数据
  5. Android源码阅读分析:从Activity开始(二)——加载布局
  6. Android UI框架 Android UI控件类简介 android5大布局详解
  7. Android 动态加载布局文件

随机推荐

  1. Android解决HAXM安装的问题
  2. android中gps的打开关闭
  3. Android(安卓)去掉toolbar阴影
  4. android 获取联系人和短消息信息
  5. android 邮件地址正则表达式
  6. Android之ScrollView嵌套ListView解决工
  7. Android(安卓)文件操作
  8. android客户端与web服务端的数据通信
  9. android studio 导入其他project遇到 Cou
  10. Android(安卓)Error: java.lang.IllegalA