Android学习|控件—— AlertDialog小弹窗

本文介绍了Android中如何使用AlertDialog来创建消息框,包括设置图标、标题、消息内容、自定义布局,以及添加确定、取消和中间按钮,并通过实例展示了如何在布局中嵌套其他视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

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
        }

效果
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值