自定义样式 dialog

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义样式 dialog相关的知识,希望对你有一定的参考价值。

 

自定义样式 dialog,可设置界面外点击屏幕外和返回键  是否消失

基本用法如下

        CustomDialog.Builder customBuilder = new CustomDialog.Builder(context);
        customBuilder.setTitle(title)
                .setMessage(content)
                .setNegativeButton(cancelText, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (cancelListener != null)
                            cancelListener.onClick(dialog, which);
                    }
                })
                .setPositiveButton(okText,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                if (okListener != null)
                                    okListener.onClick(dialog, which);
                            }
                        });
        CustomDialog dialog = customBuilder.create();
        dialog.setCancelable(isAutoDissmiss);
        dialog.show();

 

Utils 工具类封装

CustomDialogUtil.java
/**
 * Created by dingzuoqiang on 2017/6/20.
 * Email: [email protected]
 */

public class CustomDialogUtil {

    /**
     * final Context context,
     * String content,
     * String okText,
     * String cancelText,
     * final DialogInterface.OnClickListener okListener
     * final DialogInterface.OnClickListener cancelListener
     * boolean isAutoDissmiss
     */
    public static View normalDialog(final Context context, String title, String content, String okText, String cancelText, final DialogInterface.OnClickListener okListener, final DialogInterface.OnClickListener cancelListener, boolean isAutoDissmiss) {

        CustomDialog.Builder customBuilder = new CustomDialog.Builder(context);
        customBuilder.setTitle(title)
                .setMessage(content)
                .setNegativeButton(cancelText, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (cancelListener != null)
                            cancelListener.onClick(dialog, which);
                    }
                })
                .setPositiveButton(okText,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                if (okListener != null)
                                    okListener.onClick(dialog, which);
                            }
                        });
        CustomDialog dialog = customBuilder.create();
        dialog.setCancelable(isAutoDissmiss);
        dialog.show();

        return dialog.getWindow().getDecorView();
    }

}

 

 
CustomDialogUtil.normalDialog(MainActivity.this, "Title", "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊", "确定", "取消", onClickListener, null, true);

 

效果图:

技术分享

 

git 源码下载

 

To get a Git project into your build:

Step 1. Add the JitPack repository to your build file

    allprojects {
        repositories {
            ...
            maven { url ‘https://jitpack.io‘ }
        }
    }

Step 2. Add the dependency

    dependencies {
            compile ‘com.github.dingzuoqiang:CustomDialog:v1.0‘
    }

 

 

CustomDialog.Builder customBuilder = new CustomDialog.Builder(context);
customBuilder.setTitle(title)
.setMessage(content)
.setNegativeButton(cancelText, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (cancelListener != null)
cancelListener.onClick(dialog, which);
}
})
.setPositiveButton(okText,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (okListener != null)
okListener.onClick(dialog, which);
}
});
CustomDialog dialog = customBuilder.create();
dialog.setCancelable(isAutoDissmiss);
dialog.show();





















以上是关于自定义样式 dialog的主要内容,如果未能解决你的问题,请参考以下文章

Android中自定义Dialog样式

怎么自定义dialog样式

如何自定义Android Dialog的样式?

自定义对话框片段

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

自定义样式 dialog