美文网首页Android开发知识小集
基础AlertDialog(提示类控件)

基础AlertDialog(提示类控件)

作者: testjianshuandr | 来源:发表于2019-01-03 19:14 被阅读184次


    目录

    1.什么是AlertDialog

      AlertDialog为显示提示信息的第三个控件,一般在程序代码中创建而不在XML文件中创建,同时它也是其他 Dialog的的父类!比如ProgressDialog,TimePickerDialog等,而AlertDialog的父类是:Dialog!

      当AlertDialog 出现在界面后,其能够屏蔽掉其他所有控件的交互能力,因此AlertDialog通常用在提示用户、警告信息等场景。

      AlertDialog并不能直接new出来,如果你打开 AlertDialog的源码,会发现构造方法是protected的,如果我们要创建AlertDialog的话,我们 需要使用到该类中的一个静态内部类:public static class Builder,调用Builder中的create方法创建AlterDialog对象, 然后来调用AlertDialog 里的相关方法,来对AlertDialog进行定制,最后调用AlterDialog的show()方法来显示我们的AlertDialog对话框!


    2.实现AlertDialog的步骤

    Step 1:创建AlertDialog.Builder对象;

    Step 2:调用setIcon()设置图标,setTitle()setCustomTitle()设置标题;

    Step 3:设置对话框的内容:setMessage()还有其他方法来指定显示的内容;

    Step 4:调用setPositive/Negative/NeutralButton()设置:确定,取消,中立按钮;

    Step 5:调用create()方法创建这个对象,再调用show()方法将对话框显示出来;


    3.日常开发中常用的AlertDialog实例

    ①普通的AlertDialog

    普通AlertDialog 代码实现

    ②带普通列表的AlertDialog

    带普通列表的AlertDialog 代码实现


    ③带单选列表的AlertDialog

    带单选列表的AlertDialog 代码实现


    ④复选框列表的AlertDialog

    复选框列表的AlertDialog 代码实现

    以上是一些在开发中AlertDialog的常见用法。另外,为了防止点击对话框的外部区域,对话框就会消失,我们 可以为builder设置setCancelable(false)即可解决这个问题!

    ---------------------------------END-------------------------------

    相关文章

      网友评论

        本文标题:基础AlertDialog(提示类控件)

        本文链接:https://www.haomeiwen.com/subject/vteqrqtx.html