Dialog(一)------基础应用篇

本文介绍如何在Android中创建自定义对话框(Dialog),包括设置标题、消息内容、图标及按钮,以及如何统一设置按钮监听器,并展示了如何使用自定义布局。

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


前言:最近在做列表的删除功能,应为Android的滑动删除不是那么轻松就能够完成,虽然也可以实现,但是担心后期内容模块的更新,会增加一些更复杂的滑动一些列操作,因此采用点击按钮删除,之后弹出提示框来确认用户是否删除,不排除误点情况,然后发现原生的Dialog是太丑了,于是便小小研究了一番,也查看一些人博客,再次特此感谢。。,下面就开始,最基础的dialog吧。

一、最简单的dialog(标题和提示内容)

1、首先创建一个Dialog:
在构造方法里有四种:

(1)Dialog dialog = new Dialog(this);
(2)AppCompatDialog appCompatDialog = new AppCompatDialog(this);
(3)android.support.v7.app.AlertDialog   alertDialog=new android.support.v7.app.AlertDialog();
(4)android.app.AlertDialog alertDialog1 = new android.app.AlertDialog(this);

这四种方法,其中(2)是继承(1),(3)是继承(2),(4)直接继承(1),由于(1)(2)的功能较单一(个人观点),因此选择使用(3)(4),
然而(3)(4)的构造是方法是protected关键词修饰,无法直接引用,因此只能使用推荐的设计模式的builder模式来进行构造,以(3)为例:

android.support.v7.app.AlertDialog.Builder builder=new android.support.v7.app.AlertDialog.Builder(this);
        builder.setTitle("title");//设置标题
        builder.setMessage("message");//设置信息内容
        android.support.v7.app.AlertDialog alertDialog = builder.create();//通过builder创建dialog
        alertDialog.show();//显示dialog
        alertDialog.dismiss();//取消dialog

先创建一个Builder,让后通过builder来设置dilog的标题 、内容等,通过create来构造出alertDialog,在通过alertDialog的show方法来显示dialog,dismiss来取消dialog。
友情提示:在使用v7包里的AlertDialog时,其构造方法有:

 (1)protected AlertDialog(Context context)
 (2)protected AlertDialog(Context context, @StyleRes int themeResId)
 (3)protected AlertDialog(Context context, boolean cancelable,OnCancelListener cancelListener)

在本例中我们采用的是第(1)种,只传了上下文的引用,(2)则需要另外传一个AlertDialog的样式,(3)和(1)一样不需要传入相应样式,这里要注意,之所不用传入样式,是因为它会自动解析传入的context的样式,如果传入context所持有的样式不是Them.AppCompat则会报错:

Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

解决方法:改变context的样式或者利用第二中方法手动给添加一个Them.AppCompat样式。
2、按钮和小图标
(1)小图标:通过builer.setIcon()会在标题的左侧添加一个小图标;
(2)按钮:google为我们提供了三个选择按钮,左按钮,右按钮,中间按钮,分别对应三个设置方法:

Builder.setPositiveButton(string,DialogInterface.OnClickListener);//确定按钮
Builder.setNegativeButton(string,DialogInterface.OnClickListener);//取消按钮
Builder.setNeutralButton(string,DialogInterface.OnClickListener);//中间按钮

友情提示,默认的三个按钮,中间按钮无论把它放在代码的什么位置,她始终都是在中间的,和代码的设置的顺序无关,其他两个按钮也是一样,和代码的位置是无关的,不会因为在代码中先谢了确定按钮,在现实时确定按钮就在左边,不是这样,他和系统的样式是相关联的,系统样式中定义的确定按钮在那边显示他就会在哪边显示!贴出相对完整代码:

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this, R.style.mydialog);
        builder.setIcon(R.mipmap.ic_launcher_round);//添加小图标icon
        builder.setTitle("title");//设置标题
        builder.setMessage("message");//设置信息内容
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNeutralButton("中间", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "中间", Toast.LENGTH_SHORT).show();
            }
        });
        android.support.v7.app.AlertDialog alertDialog = builder.create();//通过builder创建dialog
        alertDialog.show();//显示dialog

效果图不在给出,实践检验真理,请自行尝试查看效果!
3、三个按钮的统一监听,在上面 分别对三个按钮进行设置监听,有点不优雅,于是系统有提供了一个相应的监听方法来统一,实现更优雅的监听:

public interface DialogInterface { 

 /**
     * The identifier for the positive button.
     */
    public static final int BUTTON_POSITIVE = -1;

    /**
     * The identifier for the negative button. 
     */
    public static final int BUTTON_NEGATIVE = -2;

    /**
     * The identifier for the neutral button. 
     */
    public static final int BUTTON_NEUTRAL = -3;
    
	interface OnClickListener {
	        public void onClick(DialogInterface dialog, int which);
	    }
    }

我们只需要实现这个接口,并对每个按钮进行设置就可以了,其中参数which映射的是BUTTON_POSITIVE、BUTTON_NEGATIVE、BUTTON_NEUTRAL三个按钮,具体代码如下:

public void makeDialog3(View view) {
        android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this, R.style.mydialog);
        builder.setIcon(R.mipmap.ic_launcher_round);//添加小图标icon
        builder.setTitle("title");//设置标题
        builder.setMessage("message");//设置信息内容
        builder.setPositiveButton("确定", onClickListener);
        builder.setNegativeButton("取消", onClickListener);
        builder.setNeutralButton("中间", onClickListener);
        android.support.v7.app.AlertDialog alertDialog = builder.create();//通过builder创建dialog
        alertDialog.show();//显示dialog
    }

    private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case DialogInterface.BUTTON_POSITIVE:
                    Toast.makeText(MainActivity.this, "确定按钮", Toast.LENGTH_SHORT).show();
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    Toast.makeText(MainActivity.this, "取消按钮", Toast.LENGTH_SHORT).show();
                    break;
                case DialogInterface.BUTTON_NEUTRAL:
                    Toast.makeText(MainActivity.this, "中间按钮", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };

二、自定义布局:

我们这里只进行静态的布局方式(通过xml来布局),动态布局不做讲解(通过代码来创建布局并添加相应的控件模块)

public Builder setView(int layoutResId) ;
public Builder setView(View view) ;

我们可以通过设置View或者布局文件id来进行自定义布局,例如文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_margin="30dp"
    android:orientation="vertical"
    app:cardCornerRadius="20dp"
    app:cardElevation="10dp"
    app:cardBackgroundColor="@android:color/holo_blue_bright"
    app:contentPadding="15dp">

   <LinearLayout
       android:layout_width="match_parent"
       android:orientation="horizontal"
       android:layout_height="match_parent">

       <ImageView
           android:layout_width="wrap_content"
           android:src="@mipmap/ic_launcher"
           android:layout_gravity="center_vertical"
           android:layout_height="wrap_content"/>
       <TextView
           android:layout_width="wrap_content"
           android:layout_gravity="center_vertical"
           android:gravity="center"
           android:layout_height="match_parent"
           android:text="mydialog"/>
   </LinearLayout>


</android.support.v7.widget.CardView>

(1)setView(View view):使用设置View来自定义:


android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this, R.style.mydialog);
        View myView = LayoutInflater.from(this).inflate(R.layout.myview, null);
        TextView content = (TextView) myView.findViewById(R.id.content);
        content.setText("设置你要设置的内容");
        builder.setView(content);
        android.support.v7.app.AlertDialog alertDialog = builder.create();//通过builder创建dialog
        alertDialog.show();//显示dialog

(2)setView(int layoutResId),设置布局ID来定义:

android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(this, R.style.mydialog);
        builder.setView(R.layout.myview);
        TextView content = (TextView) findViewById(R.id.content);
        content.setText("设置你要设置的内容");
        builder.setView(content);
        android.support.v7.app.AlertDialog alertDialog = builder.create();//通过builder创建dialog
        alertDialog.show();//显示dialog

其效果图为:
其效果图为:
好了,这篇到这就结束了,下篇给大家详细介绍一些google给我们分装好的一下AlertDialog。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值