Android开发学习笔记——对话框Dialog
弹出对话框在我们进行开发的过程中是一个很常见的需求,比如在退出APP之前、或者执行某种操作之前都会弹出对话框与用户进行交互,请求确认。
基本使用
常用属性和方法
AlertDialog
AlertDialog是在系统提供的Dialog中我们使用最多和功能最强大的一个Dialog,其基本用法很简单,只需要通过AlertDialog.Builder创建Builder对象,通过Builder为对话框设置标题、图标、按钮等内容,然后调用builder.create()方法创建dialog,使用dialog.show()即可显示对话框。其基本样式如下图:
基本方法和使用
AlertDialog的使用方式很简单,其标签、图标、按钮都是只需要设置相关文字、点击事件等即可,而中间内容部分,可以设置为文字内容、列表、单选列表和多选列表等,这都可以通过Builder类直接实现。Buider类常用方法如下:
方法名 | 说明 |
---|---|
setTitle | 设置标题 |
setIcon | 设置图标 |
setMessage | 设置对话框提示内容 |
setItems | 设置显示列表对话框 |
setAdapter | 设置对话框列表adapter |
setView | 设置对话框自定义view |
setMultiChoiceItems | 设置多选列表对话框 |
setSingleChoiceItems | 设置单选列表 |
setPositiveButton | 设置positive按钮内容和点击事件 |
setPositiveButton | 设置positive按钮内容和点击事件 |
setNegativeButton | 设置negative按钮内容和点击事件 |
setNeutralButton | 设置Neutral按钮内容和点击事件 |
setCancelable | 设置对话框是否能够退出 |
setCustomTitle | 设置对话框自定义view |
setCursor | 设置cursor中一个列用对话框列表显示 |
create | 创建一个对话框 |
AlertDialog的一些常用方法:
方法名 | 说明 |
---|---|
show | 显示对话框 |
dismiss | 隐藏对话框 |
cancel | 隐藏对话框同时回调onCancelListener中的退出事件 |
setOnCancleListener | 设置退出对话框监听 |
setCanceledOnTouchOutside | 设置点击对话框外部是否退出对话框 |
值得注意的是:在没有设置退出监听前,dismiss和cancel是完全相同的。
AlertDialog的基本使用方法如下代码所示:
private fun showAlertDialog(){
val builder = AlertDialog.Builder(this)
.setTitle("标题")
.setMessage("测试对话框")
.setIcon(R.mipmap.ic_launcher_round)
.setPositiveButton("确定") {
dialogInterface: DialogInterface, i: Int ->
dialogInterface.dismiss()
Log.e("dialog","点击确定按钮")
}
.setNegativeButton("取消") {
dialogInterface: DialogInterface, i: Int ->
dialogInterface.dismiss()
Log.e("dialog", "点击取消按钮")
}
.setNeutralButton("忽略"){
dialogInterface: DialogInterface, i: Int ->
dialogInterface.dismiss()
Log.e("dialog", "点击忽略按钮")
}
val dialog = builder.create()
dialog.setCanceledOnTouchOutside(true)
dialog.show()
}
显示效果如下:
列表对话框
其实根据上述对Builder的描述我们很容易就能猜测到,只有使用setItems方法就可以很轻松地创建一个带列表地对话框。
private fun showAlertListDialog(){
val list = arrayOf<String>("张三","李四","王五","赵六","钱七","小明","小华")
val builder = AlertDialog.Builder(this)
.setTitle("列表")
.setItems(list) {
dialogInterface: DialogInterface, i: Int ->
Log.e("dialog", "点击item$i")
}
.setIcon(R.mipmap.ic_launcher_round)
.setPositiveButton("确定") {
dialogInterface: DialogInterface, i: Int ->
dialogInterface.dismiss()
Log.e("dialog","点击确定按钮")
}
val dialog = builder.create()
dialog.setCanceledOnTouchOutside(true)
dialog.show()
}
显示效果如下,点击列表项对话框会自动消失:
需要注意地是,在使用setItems方法时不能再使用setMessage,否则只会显示文本内容,不会显示列表。
但是这种方法只能显示最简单地字符串列表,我们也可以使用setAdapter方法来在对话框中实现列表对话框,通过设置Adapter,我们能够实现更加复杂地列表出来。
具体实现方法,和List View地实现方法相同,只需要自定义Adapter或者使用系统提供地ArrayAdapter、SimpleAdapter,然后使用setAdapter方法进行设置即可。
class MineAdapter(var mContext: Context, var data: MutableList<ItemData>) : BaseAdapter() {
companion object{
const val TYPE_NORMAL = 0
const val TYPE_IMAGE = 1
}
@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
//获取item布局
val view = LayoutInflater.from(mContext).inflate(R.layout.item_mine,parent, false)
//展示item数据
val tvName = view.findViewById<TextView>(R.id.tvName)
val tvAge= view.findViewById<TextView>(R.id.tvAge)
val ivAvatar = view.findViewById<ImageView>(R.id.ivAvatar)
val btTest = view.findViewById<Button>(R.id.btTest)
tvName.text = data[position].name//设置名字
tvAge.text = data[position].age.toString()//设置年龄
ivAvatar.setImageResource(R.mipmap.ic_launcher)//设置头像
// btTest.setOnClickListener { Log.e("test", "show button${position}") }
// view.setOnClickListener { Log.e("test", "show item${position}") }
return view
}
override fun getItemViewType(position: Int): Int {
return data[position].type
}
override fun getViewTypeCount(): Int {
return 2
}
override fun getItem(p0: Int): Any {
return data[p0]
}
override fun getItemId(p0: Int): Long {
return p0.toLong()
}
override fun getCount(): Int {
return data.size
}
}
对话框实现如下:
private fun showAlertListAdapterDialog(){
val list = mutableListOf<ItemData>()
for (i in 1..20) list.add(ItemData(MineAdapter.TYPE_NORMAL,"item-name$i", 10+i, R.mipmap.ic_launcher))
val builder = AlertDialog.Builder(this)
.setTitle("列表")
.setAdapter(MineAdapter(this, list)