Android 自定义Dialog的实现

article/2025/11/6 5:19:49

最新实现了一个自定义Dialog的需求,先看看效果图:

下面说说如何实现:

首先需要自定义一个Dialog类,继承自android.app.Dialog类。这个Dialog类就是要显示的对话框,包含双选按钮和单选按钮两种效果。本例中自定义CustomDialog类的代码先贴上:

package example.lwc.com.demo2;import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;public class CustomDialog extends Dialog {public CustomDialog(Context context) {super(context);}public CustomDialog(Context context, int theme) {super(context, theme);}public static class Builder {private String message;private View contentView;private String positiveButtonText;private String negativeButtonText;private String singleButtonText;private View.OnClickListener positiveButtonClickListener;private View.OnClickListener negativeButtonClickListener;private View.OnClickListener singleButtonClickListener;private View layout;private CustomDialog dialog;public Builder(Context context) {//这里传入自定义的style,直接影响此Dialog的显示效果。style具体实现见style.xmldialog = new CustomDialog(context, R.style.Dialog);     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);layout = inflater.inflate(R.layout.dialog_layout, null);dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));}public Builder setMessage(String message) {this.message = message;return this;}public Builder setContentView(View v) {this.contentView = v;return this;}public Builder setPositiveButton(String positiveButtonText, View.OnClickListener listener) {this.positiveButtonText = positiveButtonText;this.positiveButtonClickListener = listener;return this;}public Builder setNegativeButton(String negativeButtonText, View.OnClickListener listener) {this.negativeButtonText = negativeButtonText;this.negativeButtonClickListener = listener;return this;}public Builder setSingleButton(String singleButtonText, View.OnClickListener listener) {this.singleButtonText = singleButtonText;this.singleButtonClickListener = listener;return this;}/*** 创建单按钮对话框* @return*/public CustomDialog createSingleButtonDialog() {showSingleButton();layout.findViewById(R.id.singleButton).setOnClickListener(singleButtonClickListener);//如果传入的按钮文字为空,则使用默认的“返回”if (singleButtonText != null) {((Button) layout.findViewById(R.id.singleButton)).setText(singleButtonText);} else {((Button) layout.findViewById(R.id.singleButton)).setText("返回");}create();return dialog;}/*** 创建双按钮对话框* @return*/public CustomDialog createTwoButtonDialog() {showTwoButton();layout.findViewById(R.id.positiveButton).setOnClickListener(positiveButtonClickListener);layout.findViewById(R.id.negativeButton).setOnClickListener(negativeButtonClickListener);//如果传入的按钮文字为空,则使用默认的“是”和“否”if (positiveButtonText != null) {((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);} else {((Button) layout.findViewById(R.id.positiveButton)).setText("是");}if (negativeButtonText != null) {((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);} else {((Button) layout.findViewById(R.id.negativeButton)).setText("否");}create();return dialog;}/*** 单按钮对话框和双按钮对话框的公共部分在这里设置*/private void create() {if (message != null) {      //设置提示内容((TextView) layout.findViewById(R.id.message)).setText(message);} else if (contentView != null) {       //如果使用Builder的setContentview()方法传入了布局,则使用传入的布局((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));}dialog.setContentView(layout);dialog.setCancelable(true);     //用户可以点击手机Back键取消对话框显示dialog.setCanceledOnTouchOutside(false);        //用户不能通过点击对话框之外的地方取消对话框显示}/*** 显示双按钮布局,隐藏单按钮*/private void showTwoButton() {layout.findViewById(R.id.singleButtonLayout).setVisibility(View.GONE);layout.findViewById(R.id.twoButtonLayout).setVisibility(View.VISIBLE);}/*** 显示单按钮布局,隐藏双按钮*/private void showSingleButton() {layout.findViewById(R.id.singleButtonLayout).setVisibility(View.VISIBLE);layout.findViewById(R.id.twoButtonLayout).setVisibility(View.GONE);}}
}

Dialog所用的布局文件dialog_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:clickable="true"android:orientation="vertical"android:paddingTop="120dp"android:paddingBottom="120dp" ><LinearLayout
        android:layout_width="match_parent"android:layout_height="230dp"android:layout_gravity="center"android:background="@drawable/dialog_bg_biggg"android:padding="40dp"android:orientation="vertical" ><LinearLayout
            android:id="@+id/content"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="40dp"android:gravity="center" ><TextView
                android:id="@+id/message"style="@style/text_16_666666"android:text="提示信息在这里"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="left|center"android:lineSpacingMultiplier="1.5"android:minHeight="50dp"android:paddingLeft="20dp"android:paddingRight="20dp" /></LinearLayout><FrameLayout
            android:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayout
                android:id="@+id/twoButtonLayout"android:visibility="visible"android:layout_width="match_parent"android:layout_height="60dp"android:layout_gravity="bottom"android:gravity="center"android:orientation="horizontal" ><Button
                    android:id="@+id/positiveButton"style="@style/text_15_ffffff_sdw"android:layout_width="0dp"android:layout_weight="1"android:layout_height="45dp"android:background="@drawable/dialog_ok"android:gravity="center" /><Button
                    android:id="@+id/negativeButton"style="@style/text_15_666666_sdw"android:layout_width="0dp"android:layout_weight="1"android:layout_height="45dp"android:background="@drawable/dialog_cancel"android:gravity="center" /></LinearLayout><LinearLayout
                android:id="@+id/singleButtonLayout"android:visibility="visible"android:layout_width="match_parent"android:layout_height="60dp"android:layout_gravity="bottom"android:paddingLeft="50dp"android:paddingRight="50dp"android:paddingBottom="10dp"android:gravity="center"><Button
                    android:id="@+id/singleButton"style="@style/text_15_ffffff_sdw"android:layout_width="wrap_content"android:layout_height="40dp"android:background="@drawable/dialog_back"android:gravity="center" /></LinearLayout></FrameLayout></LinearLayout></FrameLayout>

以及style.xml:

<resources><style name="AppBaseTheme" parent="android:Theme.Light"></style><style name="AppTheme" parent="AppBaseTheme"></style><style name="text_18_ffffff"><item name="android:textSize">18.0dip</item><item name="android:textColor">#777733</item></style><style name="text_16_666666"><item name="android:textSize">16.0dip</item><item name="android:textColor">#ff666666</item></style><style name="sdw_white"><item name="android:shadowColor">#7fffffff</item><item name="android:shadowDx">0.0</item><item name="android:shadowDy">0.65</item><item name="android:shadowRadius">1.0</item></style><style name="sdw_79351b"><item name="android:shadowColor">#ff79351b</item><item name="android:shadowDx">0.0</item><item name="android:shadowDy">1.0</item><item name="android:shadowRadius">1.0</item></style><style name="text_15_ffffff_sdw" parent="@style/sdw_79351b"><item name="android:textSize">15.0dip</item><item name="android:textColor">#ffffffff</item></style><style name="text_15_666666_sdw" parent="@style/sdw_white"><item name="android:textSize">15.0dip</item><item name="android:textColor">#ff666666</item></style><style name="Dialog" parent="android:style/Theme.Dialog"><item name="android:background">#00000000</item><item name="android:windowBackground">@android:color/transparent</item><item name="android:windowNoTitle">true</item><item name="android:windowIsFloating">true</item></style>
</resources>

在Android studio中,预览效果如下:

最后看看在Activity中如何调用:

1.先在Activity中定义一个Builder和一个CustomDialog实例:

    private CustomDialog.Builder builder;private CustomDialog mDialog;

在onCreate()中初始化builder:

builder = new CustomDialog.Builder(this);

2.在MainActivity中定义两个方法,分别用于显示单按钮对话框和双按钮对话框:

private void showSingleButtonDialog(String alertText, String btnText, View.OnClickListener onClickListener) {mDialog = builder.setMessage(alertText).setSingleButton(btnText, onClickListener).createSingleButtonDialog();mDialog.show();
}
private void showTwoButtonDialog(String alertText, String confirmText, String cancelText, View.OnClickListener conFirmListener, View.OnClickListener cancelListener) {mDialog = builder.setMessage(alertText).setPositiveButton(confirmText, conFirmListener).setNegativeButton(cancelText, cancelListener).createTwoButtonDialog();mDialog.show();
}

3.在需要用到的地方调用上面的两个方法即可,比如弹出双选按钮对话框,可以像下面这样调用:

    showTwoButtonDialog("这是双选对话框的内容!", null, null, new View.OnClickListener() {@Overridepublic void onClick(View v) {mDialog.dismiss();//这里写自定义处理XXX}}, new View.OnClickListener() {@Overridepublic void onClick(View v) {mDialog.dismiss();//这里写自定义处理XXX}});

弹出单选按钮对话框,可以这样调用:

    showSingleButtonDialog("这是单选对话框的内容!", null, new View.OnClickListener() {@Overridepublic void onClick(View v) {mDialog.dismiss();//这里写自定义处理XXX}});

这里传入的按钮文字都为null,所以显示默认文字,结果就如开头的图所示。

另外谈一下自己的心得:
在自定义的CustomDialog类中,为什么要用一个静态内部类Builder呢?
因为对话框要设置的属性太多,包括各个按钮的文字,对话框内容,标题(本例中未实现标题),点击效果listener等。如果不用Builder,要创建一个自定义的Dialog,这些属性一般是要作为构造方法的参数传入的。但麻烦的是参数不固定,有些参数可传可不传,比如我们想new 一个单按钮对话框,那么双按钮的文字和Listener是不需要传入的。这样的话我们需要实现多个不同参数的构造方法,或者调用多次set方法设置属性。比起那样,像本例中这样用Builder就方便得多了,直观简洁:

    mDialog = builder.setMessage(alertText).setPositiveButton(confirmText, conFirmListener).setNegativeButton(cancelText, cancelListener).createTwoButtonDialog();

http://chatgpt.dhexx.cn/article/bJ4x0bnC.shtml

相关文章

Android自定义Dialog对话框的几种方法(精简版)

自定义对话框是经常使用的功能&#xff0c;我们常用的弹窗操作&#xff0c;除了使用popwindow就是使用dialog来实现&#xff0c;这两种组件都支持之定义布局和功能来满足我们个性化的需求&#xff0c;也可以不采用自定义而直接使用系统封装好的api来实现功能。今天简单总结下在…

自定义Dialog的简单实现

自定义Dialog的详细步骤&#xff08;实现自定义样式一般原理&#xff09; 发表于2016/3/22 22:12:57 1410人阅读 分类&#xff1a; android开发 转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自定义Dialog的详细步骤&#xff08;实现自定义样…

安卓dialog的使用+如何自定义dialog

吐槽 哇哇哇&#xff0c;刚写一半win10给我蓝屏了&#xff0c;心塞塞&#xff0c;以后写一点保存一点。回到正题&#xff0c;看到产品给我的设计图&#xff0c;有辣么多的自定义的dialog&#xff0c;发现之前自己只会系统自带的dialog&#xff0c;但是这样根本满足不了产品的需…

Android/安卓 自定义Dialog 最简单、最详细解释

看了很多视频&#xff0c;也在网上找了一些浏览量最多的文章&#xff0c;发现都太难懂或者太复杂&#xff0c;夹杂了很多其他功能&#xff0c;自定义度太高&#xff0c;很繁琐。所以我想写一个基础的自定义Dialog&#xff0c;只涉及基础的自定义&#xff0c;其他复杂的自定义可…

自定义Dialog的详细步骤(实现自定义样式一般原理)

转载请标注转载http://blog.csdn.net/oqihaogongyuan/article/details/50958659 自定义Dialog的详细步骤&#xff08;实现自定义样式一般原理&#xff09; 现在很多App的提示对话框都非常有个性&#xff0c;然而你还用系统的对话框样式&#xff0c;是不是觉得很落后呢&#xf…

CSS 文字超出部分显示省略号

一&#xff1a; title { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; } 以上 CSS 适用于单行文字超出部分&#xff0c;显示「省略号」。如图&#xff1a; 二&#xff1a; title { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-cl…

css三行代码实现多行文字超出部分省略号代替

就是我们在页面开发的时候有时为了更加美观&#xff0c;让文字在同行显示&#xff0c;超出的部分用省略号替代 而这个效果我们直接实现3行css实现 white-space: nowrap; 让文字在一行显示&#xff0c;不允许换行 overflow: hidden; 超出部分隐藏 text-overflow: ellipsis; 超…

IOS不兼容超出部分省略号 且页面显示不起作用 行数限定无作用

IOS不兼容超出部分省略号 且页面显示不起作用 行数限定无作用 换成下面的写法 overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 1;-webkit-box-orient: vertical;

CSS超出部分省略号

1&#xff0c;单行文本实现超出部分省略号 overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用省略号显示 white-space:nowrap; //溢出不换行 2&#xff0c;多行文本实现超出部分省略号 overflow:hidden; text-overflow:ellipsis; display:-webkit-b…

css文字超出部分省略号代替

问题 之前做过好过网页&#xff0c;其中经常遇到一个常用的知识点&#xff0c;就是当文字超出界面的范围&#xff0c;用省略号代替显示&#xff0c;给网页一美观。 解决方案 1.单行显示省略号&#xff1a; 点击查看代码详情 效果如下: 2.多行显示省略号&#xff1a; 点击…

CSS文字超出部分省略号显示

文章目录[隐藏] CSS 文字超出部分省略号显示前言CSS 文字超出部分省略号显示实现方法CSS 文字超出部分省略号显示实现效果 CSS 文字超出部分省略号显示前言 在我们开发过程中&#xff0c;其实有这样的需求&#xff1a; 在一个文章列表中&#xff0c;文章摘录太长了会影响排版格…

css 文字换行和超出部分省略号

css 文字换行和超出部分省略号 文字空格与换行 当文本溢出父级 block 元素时, 文本会换行; 这个特性是通过 white-space 控制的; 它影响浏览器处理空格, 换行和 tab 的处理; 它有下面几种值 normal, 默认值, 连续的空白符会被合并&#xff0c;换行符会被当作空白符来处理。…

overflow超出部分省略号

实现上图所示样式 单行超市省略号 width: 9rem;overflow: hidden;text-overflow: ellipsis;white-space: nowrap; //单行超出显示省略号可设置多行超出省略 //两行超出.overhide {display: -webkit-box !important;text-overflow: ellipsis;overflow: hidden;-webkit-line-cla…

html字体超出后隐藏省略号,CSS控制文字超出部分省略号显示

一:单行文本显示不完时,我们只需要设置单行文本的宽度,不允许换行,溢出隐藏及换行省略四个属性即可: p{max-width: 100px; overflow:hidden; text-overflow:ellipsis; /*禁止换行显示*/ white-space:nowrap; background-color:#ffe51a; } 哈哈哈哈哈哈哈,哈哈哈哈哈,哈哈…

文本超出部分显示省略号

我们经常在网站上可以看到以下样式&#xff0c;标题太长&#xff0c;一行显示不下&#xff0c;则会使用省略号来代替。但是事实上&#xff0c;这个省略号并不是打字打上去的&#xff0c;而是使用代码表示出来的。 今天则主要介绍如何让文本超出部分显示省略号。 1.单行文本超出…

CSS中 设置( 单行、多行 )超出显示省略号

1. 设置超出显示省略号 css设置超出显示省略号可分两种情况&#xff1a; 单行文本溢出显示省略号…多行文本溢出显示省略号… 但使用的核心代码是一样的&#xff1a;需要先使用 “overflow:hidden;” 来把超出的部分隐藏&#xff0c;然后使用“text-overflow:ellipsis;”当文…

文本超出显示省略号的方法

1、一行文本超出显示省略号的方法&#xff1a;text-overflow和white-space超出隐藏显示省略号 设计css样式时&#xff0c;遇到要使文本在一行内显示&#xff0c;超出则加省略号的问题解决办法&#xff1a; 只需要使用text-overflow和white-space来使文本在一行内显示&#xff…

文字超出部分变成省略号的三种方式

目录 1.单行文本溢出显示省略号 实例&#xff1a; 2.多行文本溢出显示省略号 实例&#xff1a; 3.利用伪类实现省略号 实例&#xff1a; 1.单行文本溢出显示省略号 .box {/*强制文本在一行内显示*/white-space: nowrap; overflow: hidden;text-overflow: ellipsis; } 实例&a…

pytorch函数中的dilation参数的作用

举例子说明&#xff1a; 1.dilation1的话&#xff08;默认情况&#xff09;&#xff0c;效果如图&#xff1a; 2.dilation2&#xff0c;那么效果如图&#xff1a; 输入是蓝色&#xff0c;绿色是输出&#xff0c;可以看到dilation1时输入间隔着一个格子。

Pytorch 网络中dilation=0和dilation=N (N=1,2,3,...)的区别

假设输入为4x4&#xff0c;卷积核为3x3&#xff0c;stride1&#xff0c;此时dilation1的卷积情况&#xff1a; 下面是输入为7x7&#xff0c;卷积核为3x3&#xff0c;stride1&#xff0c;dilation2时候的卷积情况 在这种情况下&#xff0c;卷积核的感受野由原来的3x3变成了现在…