Android学习|控件—— AlertDialog弹窗
概述
AlertDialog:用于弹出消息框,消息框可定义按钮,也可自定义布局。
事例如下:点击Onclick,弹出如下小窗口
实现方式
1、AlertDialog.Builder builder = new AlertDialog.Builder(context) :构建Dialog的各种参数
2、Builder.setlcon(int iconld): 添加ICON、设置图标
3、Builder.setTitle(CharSequence title):添加标题
4、Builder.setMessage(CharSequence message):添加消息
5、Builder.setView(View view):设置自定义布局
6、Builder.create():创建Dialog
7、Builder.show():显示对话框
8、setPositiveButton:确定按钮
9、setNegativeButton:取消按钮
10、setNeutralButton:中间按钮
实现AlertDialog
xml中定义button,定义点击事件btnOnclick()
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<Button
android:text="Onclick"
android:onClick="btnOnclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
Java中进行设置
public void btnOnclick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); //构造器创AlertDialog构造器
builder.setIcon(android.R.drawable.ic_lock_idle_alarm) // 设置图标
.setTitle("Title")
.setMessage("This is Message")
.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮及点击事件
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG,"确定按钮点击事件"+i); //返回-1
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮及点击事件
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG,"取消按钮点击事件"+i); //返回-2
}
})
.setNeutralButton("中间", new DialogInterface.OnClickListener() { // 设置中间按钮及点击事件
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG,"中间按钮点击事件"+i); //返回-3
}
})
.create() // 创建AlertDialog
.show(); // 展示AlertDialog
}
其中 AlertDialog.Builder builder = new AlertDialog.Builder(this); 是构造器创建AlertDialog对象。
中间部分为进行相关的set操作
此时AlertDialog仍不会显示,需要最后进行 .create() 创建 .show()展示AlertDialog
确定、取消、中间按钮会返回不同的值,可进行后续操作。
效果如下
弹窗中进行其他布局的嵌套设置
此功能借助 .setView() 方法实现
先获取自定义的其他布局
View dialogView = getLayoutInflater().inflate(R.layout.diolog_view, null);
然后对构造器进行设置
build.setView(dialogView)
完整如下:
1.主页面 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<Button
android:text="Onclick"
android:onClick="btnOnclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
2.新建xml进行自定义布局(diolog_view.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#77115463"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试测试"
android:textSize="50dp">
</TextView>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@drawable/ic_baseline_group_24">
</ImageView>
</LinearLayout>
3.MainActivity.java中进行AlertDialog创建及设置(仍为主页面的button的点击事件中编码设置)
public void btnOnclick(View view) {
View dialogView = getLayoutInflater().inflate(R.layout.diolog_view, null); //获取自定义布局
AlertDialog.Builder builder = new AlertDialog.Builder(this); //构造器创AlertDialog构造器
builder.setIcon(android.R.drawable.ic_lock_idle_alarm) // 设置图标
.setTitle("Title")
.setMessage("This is Message")
.setView(dialogView) //设置自定义布局
.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮及点击事件
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG,"确定按钮点击事件"+i); //返回-1
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮及点击事件
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG,"取消按钮点击事件"+i); //返回-2
}
})
.setNeutralButton("中间", new DialogInterface.OnClickListener() { // 设置中间按钮及点击事件
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e(TAG,"中间按钮点击事件"+i); //返回-3
}
})
.create() // 创建AlertDialog
.show(); // 展示AlertDialog
}
效果