博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用这个,自定义AlertDialog在你手里都不是问题
阅读量:6577 次
发布时间:2019-06-24

本文共 7987 字,大约阅读时间需要 26 分钟。

hot3.png

最近一直在搞自定义的AlertDialog,先贴一下图。 输入图片说明

输入图片说明


输入图片说明

这几种都是自定义的AlertDialog,还支持从Service弹出。好了,话不多说,看使用方法:

CustomDialogBuilder.With(this)//上下文                        .load(R.layout.dialog_loading)//自定义的布局文件                        .setDimAmount(0.5f)//设置背景透明度                        .setCancelable(true)//是否点击区域外隐藏                        .setHeigh(DensityUtil.dip2px(this,100))//设置高度                        .setWidth(DensityUtil.dip2px(this,100))//设置宽度                        .setWidth(0.5f)//设置宽度,以屏幕宽度为基准,取百分比                        .InitView(null)//初始化View                        .InitEvent(null)//初始化事件                        .show();//显示

是不是觉得简单好多,我们关心的只有,我们该关心的,至于其他的繁琐的东西,我们不关心好伐。

我们这里看一下是怎么写的,下面会贴出GitHub链接

CustomDialogBuilder.java

import android.app.Activity;import android.app.Fragment;import android.content.Context;import android.support.annotation.LayoutRes;import android.support.v4.app.FragmentActivity;import android.support.v7.app.AlertDialog;import com.xiaolei.easyfreamwork.R;import com.xiaolei.easyfreamwork.application.ApplicationBreage;import com.xiaolei.easyfreamwork.utils.Log;/*** Created by xiaolei on 2017/10/9.*/public class CustomDialogBuilder{   private boolean isBackNet = false;   private AlertDialog.Builder builder;   private Context context;   private CustomDialogBuilder(Context context)   {       this.context = context;       if (Activity.class.isInstance(context)               || FragmentActivity.class.isInstance(context))       {           if ((Activity.class.isInstance(context) && !((Activity) context).isFinishing())                   || (FragmentActivity.class.isInstance(context) && !((FragmentActivity) context).isFinishing()))           {               builder = new AlertDialog.Builder(context);           } else           {               Log.e("LOG", "不是activity,或者activity已关闭");           }       } else       {           builder = new AlertDialog.Builder(ApplicationBreage.getInstance().getContext(),                   R.style.Base_Theme_AppCompat_Light_Dialog);           isBackNet = true;       }   }   public static CustomDialogBuilder With(Activity activity)   {       return new CustomDialogBuilder(activity);   }   public static CustomDialogBuilder With(FragmentActivity activity)   {       return new CustomDialogBuilder(activity);   }   public static CustomDialogBuilder With(Fragment fragment)   {       return new CustomDialogBuilder(fragment.getActivity());   }   public static CustomDialogBuilder With(android.support.v4.app.Fragment fragment)   {       return new CustomDialogBuilder(fragment.getActivity());   }   public static CustomDialogBuilder With(Context context)   {       return new CustomDialogBuilder(context);   }   /**    * 加载布局文件    *    * @param layout    */   public DialogInitConfig load(@LayoutRes int layout)   {       return new DialogInitConfig(layout,builder,isBackNet,context);   }   public Context getContext()   {       return context;   }}

DialogInitConfig.java

import android.content.Context;import android.graphics.Color;import android.graphics.drawable.ColorDrawable;import android.support.annotation.FloatRange;import android.support.annotation.LayoutRes;import android.support.v7.app.AlertDialog;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.View;import android.view.Window;import android.view.WindowManager;/** *  * Created by xiaolei on 2017/10/9. */public class DialogInitConfig{    private DialogInitCallBack initCallBack;    private DialogEventCallBack eventCallBack;    private int width = -1, heigh = -1;    private float dimAmount = 0f;    @LayoutRes    private int layout = -1;    private AlertDialog.Builder builder;    private boolean isBackNet = false;    private boolean cancelable = true;    private Context context;        public DialogInitConfig(@LayoutRes int layout, AlertDialog.Builder builder, boolean isBackNet, Context context)    {        this.layout = layout;        this.builder = builder;        this.isBackNet = isBackNet;        this.context = context;    }    /**     * 获取上下文     * @return     */    public Context getContext()    {        return context;    }    /**     * 初始化UI     * @param callBack     * @return     */    public DialogInitConfig InitView(DialogInitCallBack callBack)    {        initCallBack = callBack;        return this;    }    /**     * 初始化事件     * @param callBack     * @return     */    public DialogInitConfig InitEvent(DialogEventCallBack callBack)    {        eventCallBack = callBack;        return this;    }    /**     * 设置宽度,绝对的值     * @param width     * @return     */    public DialogInitConfig setWidth(int width)    {        this.width = width;        return this;    }    /**     * 设置宽度,屏幕的百分比的     * @param widthPercent     * @return     */    public DialogInitConfig setWidth(@FloatRange(from = 0.0,to = 1.0) double widthPercent)    {        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);        this.width = (int) (wm.getDefaultDisplay().getWidth() * widthPercent);        return this;    }    /**     * 设置高度     * @param heigh     * @return     */    public DialogInitConfig setHeigh(int heigh)    {        this.heigh = heigh;        return this;    }    /**     * 设置背景透明度     * @param dimAmount     * @return     */    public DialogInitConfig setDimAmount(@FloatRange(from = 0.0,to = 1.0) float dimAmount)    {        this.dimAmount = dimAmount;        return this;    }    /**     * 设置是否可以点击侧边取消     * @param cancelable     * @return     */    public DialogInitConfig setCancelable(boolean cancelable)    {        this.cancelable = cancelable;        return this;    }    /**     * 显示出来     * @return  返回一个dialog,方便dismiss     */    public AlertDialog show()    {        if (builder == null)        {            return null;        }        builder.setTitle("");        builder.setMessage("");        builder.setCancelable(cancelable);                AlertDialog alertDialog = builder.create();        if (alertDialog == null)        {            return null;        }        Window window = alertDialog.getWindow();        if (window != null)        {            WindowManager.LayoutParams params = window.getAttributes();            // 去除四角黑色背景              window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));            if (isBackNet)            {                window.setType(WindowManager.LayoutParams.TYPE_TOAST);            }            window.setAttributes(params);        }        alertDialog.show();        try        {            View view = View.inflate(alertDialog.getContext(), layout, null);            alertDialog.setContentView(view);            if (initCallBack != null)            {                initCallBack.onInit(alertDialog, view);            }            if (eventCallBack != null)            {                eventCallBack.initEvent(alertDialog, view);            }                        window = alertDialog.getWindow();            if (window != null)            {                WindowManager.LayoutParams params = window.getAttributes();                window.setGravity(Gravity.CENTER);                DisplayMetrics dm = new DisplayMetrics();                window.getWindowManager().getDefaultDisplay().getMetrics(dm);                if (width != -1)                {                    params.width = width;                }                if (heigh != -1)                {                    params.height = heigh;                }                // 设置周围的暗色系数                  params.dimAmount = dimAmount;                window.setAttributes(params);            }        } catch (Exception e)        {            e.printStackTrace();        }        return alertDialog;    }        public interface DialogInitCallBack    {        public void onInit(AlertDialog alertDialog, View view);    }    public interface DialogEventCallBack    {        public void initEvent(AlertDialog alertDialog, View view);    }}

好吧其实我也知道你们根本不会看这些代码的,我就是要贴出来。

这里贴GitHub链接 :


The End

转载于:https://my.oschina.net/xiaolei123/blog/1548009

你可能感兴趣的文章
修改HTML5 input placeholder 颜色及修改失效的解决办法
查看>>
Windows 服务器部署 asp.net core
查看>>
html5 随机数函数
查看>>
数据结构之图(2-1)【十字链表】适用于有向图
查看>>
你必须知道的简单的位操作技巧
查看>>
怎样合并排序数组(How to merge 2 sorted arrays?)
查看>>
年礼成快递企业不再接件主因:苹果产品最疯狂
查看>>
实习第三天
查看>>
Java第四次实验
查看>>
CSS3transition实现的简单动画菜单
查看>>
C语言基础回顾
查看>>
在Java中,以下关于方法重载和方法重写描述正确的是?
查看>>
Codeforces Round #315 (Div. 2A) 569A Music (模拟)
查看>>
提升代码内外部质量的22条经验(转载)
查看>>
利用Linq对集合元素合并、去重复处理
查看>>
AS3.0 Bitmap类实现图片3D旋转效果
查看>>
BZOJ1876:[SDOI2009]SuperGCD——C++高精度良心题解
查看>>
2018-2019 20165226 Exp5 MSF基础应用
查看>>
yum安装的JDK的没有配置环境变量但是在/usr/bin下面都做了软链接
查看>>
设备坐标和逻辑坐标
查看>>