由于项目的需要,系统的弹出框已经不能满足我们的需求,我们需要各式各样的弹出框,这时就需要我们去自定义弹出框了。

新建布局文件 dialog_layout.xml,将下面内容复制进去

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/shape_linearlayout"
        android:orientation="vertical"
        >

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:gravity="center_vertical"
            android:orientation="vertical" >

<TextView
                android:id="@+id/txt_dialog_message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:text="TextView" />
        </LinearLayout>

<View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/line" />

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="1dp" >

<Button
                android:id="@+id/btn_dialog_go"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="@drawable/linearlayout"
                android:text="确定"
                android:textColor="@color/blue" />

<View
                android:id="@+id/dialog_bg"
                android:layout_width="1dp"
                android:layout_height="40dp"
                android:background="@color/line" />

<Button
                android:id="@+id/btn_dialog_add"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="@drawable/linearlayout"
                android:text="取消"
                android:textColor="@color/blue" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

新建java类DialogUtil.java

public class DialogUtil {

private static DialogUtil dialogUtil;
    private Dialog dialog;
    private TextView message;
    private Button firstBtn;
    private Button secondBtn;
    private onButtnClickListener buttnClickListener;

public static DialogUtil getDialogInstance(Context context) {
        if (null == dialogUtil) {
            dialogUtil = new DialogUtil(context);
        }
        return dialogUtil;
    }

private DialogUtil(Context context) {
        initDialog(context);
    }

public void setMessage(String message) {
        if (null != message && !message.equals("")) {
            this.message.setText(message);
        }

}

private void initDialog(Context context) {
        dialog = new Dialog(context);

dialog.setContentView(R.layout.dialog_layout);
        dialog.setCanceledOnTouchOutside(false);
        Window window = dialog.getWindow();
        dialog.setCanceledOnTouchOutside(true);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = ScreenUtils.getScreenWidth(context)
                - ScreenUtils.getScreenWidth(context) / 4;
        params.height = LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
        window.setWindowAnimations(R.style.displayStyle);
        message = (TextView) dialog.findViewById(R.id.txt_dialog_message);
        firstBtn = (Button) dialog.findViewById(R.id.btn_dialog_go);
        secondBtn = (Button) dialog.findViewById(R.id.btn_dialog_add);
        firstBtn.setOnClickListener(new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener)
                    buttnClickListener.onFirstBtnClick(v);
            }
        });
        secondBtn.setOnClickListener(new OnClickListener() {

@Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (null != buttnClickListener)
                    buttnClickListener.onSecondBtnClick(v);
            }
        });
    }

public void showDialog() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

public void dissmissDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
           
        }
    }

public void setOnButtnClickListener(onButtnClickListener buttnClickListener) {
        this.buttnClickListener = buttnClickListener;
    }

public interface onButtnClickListener {
        public void onFirstBtnClick(View v);

public void onSecondBtnClick(View v);
    }

}

dialog工具类采用的是单列的模式,调用的流程是

1、先获取对象

DialogUtil.getDialogInstance(context);

2、设置文本信息

setMessage(String message);

3、设置按钮的单机事件

setOnButtnClickListener(onButtnClickListener buttnClickListener);

4、显示dialog

showDialog();

至此自定义弹出框就完成了

Android 自定义Dialog工具类的更多相关文章

  1. (转载)实例详解Android快速开发工具类总结

    实例详解Android快速开发工具类总结 作者:LiJinlun 字体:[增加 减小] 类型:转载 时间:2016-01-24我要评论 这篇文章主要介绍了实例详解Android快速开发工具类总结的相关 ...

  2. Android 软件管理工具类Utils

    Android 软件管理工具类Utils /** * Created by uilubo on 2015/9/30. * 工具类 */ public class Utils { public stat ...

  3. Android自定义 Dialog 对话框

    Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...

  4. 一个使用命令行编译Android项目的工具类

    一个使用命令行编译Android项目的工具类 简单介绍 编译apk项目须要使用的几个工具,基本都在sdk中,它们各自是(Windows系统): 1.aapt.exe 资源打包工具 2.android. ...

  5. Android 自定义Dialog类,并在Activity中实现按钮监听。

      实际开发中,经常会用到Dialog,比如退出时候会弹出是否退出,或者还有一些编辑框也会用Dialog实现,效果图如下: 开发中遇到的问题无非在于如果在Activity中监听这个Dialog中实现的 ...

  6. Android—自定义Dialog

    在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...

  7. Android自定义Dialog

    Android开发过程中,常常会遇到一些需求场景——在界面上弹出一个弹框,对用户进行提醒并让用户进行某些选择性的操作, 如退出登录时的弹窗,让用户选择“退出”还是“取消”等操作. Android系统提 ...

  8. android 开发 常用工具类

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基本都会有一大批的辅 ...

  9. Android颜色转换工具类ColorUtil

    项目中需要根据ScrollView的滚动距离来动态设置Topbar的背景透明度,网上有类似的开源库FadingActionBar,使用的是ActionBar做的.而我的项目中并没有使用ActionBa ...

随机推荐

  1. JS、jqueryie6浏览器下使用js无法提交表单的解决办法

    -----------------------JS.jqueryie6浏览器下使用js无法提交表单的解决办法---------------------------------------------- ...

  2. 解决CSS各种IE各种兼容问题(Google解决方案)

    google的 ie7 – js中是一个JavaScript库(解决IE与W3C标准的冲突的JS库),使微软的Internet Explorer的行为像一个Web标准兼容的浏览器,支持更多的W3C标准 ...

  3. android ListView中的Item有Button时候点击异常处理

    1.当ListView中有Button的时候往往会遇到很多问题,比较常见的一个问题是: 假设:在ListView中有N个Item当点击其中某个Item中的Button的时候,需要改变当前Button的 ...

  4. Ehcache(08)——可阻塞的Cache——BlockingCache

    http://haohaoxuexi.iteye.com/blog/2119737 可阻塞的Cache—BlockingCache 在上一节我们提到了显示使用Ehcache锁的问题,其实我们还可以隐式 ...

  5. 2016 icpc-camp 之旅(一)

    啦啦啦,终于前往icpccamp啦! 嗯,该死的飞机居然晚点了! 诶,晚点居然还会发赔偿金! 飞机上没什么好说的,和萌神一起看了5集龙与虎,然后就到了. 讲道理,海南航空感觉一般. 我的座位前面有个平 ...

  6. 利用KindEditor的uploadbutton实现异步上传图片

    利用KindEditor的uploadbutton实现异步上传图片 异步上传图片最经常使用的方法就是图片在iframe中上传.这样仅仅须要刷新iframe.而不用刷新整个页面.     KindEdi ...

  7. 翻译学python---《Learn Python the hard Way》---第一章 绪论

    打算学习python,但是又不想单纯地看书或是写个小项目,干脆引入很流行的翻译学习法来学习吧-         在论坛上看到了国外的一本<Learn Python the hard Way> ...

  8. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

  9. iOS开发——动画编程Swift篇&(四)CABasicAnimation动画

    CABasicAnimation动画 //CABasicAnimation-不透明度 @IBAction func cabOpacity() { let animation = CABasicAnim ...

  10. Java RESTful Web Service相关概念

    原文地址:http://1.liangtao.sinaapp.com/?p=647 接上一篇文章REST|RESTful初步认识:p=639">http://1.liangtao.si ...