码迷,mamicode.com
首页 > 其他好文 > 详细

自定义样式 dialog

时间:2017-06-26 18:03:17      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:builder   ext   context   stat   repos   turn   sans   点击   url   

 

自定义样式 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: 530858106@qq.com
 */

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

标签:builder   ext   context   stat   repos   turn   sans   点击   url   

原文地址:http://www.cnblogs.com/dingzq/p/7081610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!