弹框DialogBuilder的使用

具体设置如下:

package com.gloiot.hygooilstation.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.DigitsKeyListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gloiot.hygooilstation.R;
import com.gloiot.hygooilstation.utils.CuttedStr_two;
import com.nineoldandroids.view.ViewHelper;

/**
 * Created by admin on 2016/8/10.
 */
public class MyDialogBuilder extends Dialog  implements DialogInterface{
    public static final int SlideTop = 0;
    public static final int SlideTopDismiss = 1;

    public static final int BtnCancel = 0;
    public static final int BtnNormal = 1;

    public static final int EtText = 0;
    public static final int EtPwd = 1;
    public static final int EtNum = 2;
    public static final int EtDecimal_Tow = 3;

    public volatile static MyDialogBuilder instance;
    private Context context;
    private int wHeight, width; // 屏幕高度宽度
    private int startEffects, dismissEffects; // 开始动画效果,消失动画效果
    private View mDialogView, customView;
    private RelativeLayout mydialog_main, mydialog;
    private TextView mydialog_title, mydialog_content;
    private ImageView mydialog_icon;
    private LinearLayout mydialog_edittextlist, mydialog_buttonlist;
    private FrameLayout mydialog_custompanel;
    private View listviewSingle;
    private int listviewSingleItems;
    public MyDialogBuilder(Context context) {
        super(context);
        this.context = context;
    }
    public MyDialogBuilder(Context context, int theme) {
        super(context, theme);
        this.context = context;
        init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes((WindowManager.LayoutParams) params);
    }

    public static MyDialogBuilder getInstance(Context context) {
//        if (instance == null) {
//            synchronized (MyDialogBuilder.class) {
//                if (instance == null) {
        instance = new MyDialogBuilder(context, R.style.dialog_untran);
//                }
//            }
//        }
        return instance;
    }

    public void init() {
        wHeight = getScreenHeight();
        width = getScreenWidth();
        mDialogView = View.inflate(context, R.layout.mydialog, null);
        mydialog_main = (RelativeLayout) mDialogView.findViewById(R.id.mydialog_main);
        mydialog = (RelativeLayout) mDialogView.findViewById(R.id.mydialog);
        mydialog_icon = (ImageView) mDialogView.findViewById(R.id.mydialog_icon);
        mydialog_title = (TextView) mDialogView.findViewById(R.id.mydialog_title);
        mydialog_content = (TextView) mDialogView.findViewById(R.id.mydialog_content);
        mydialog_buttonlist = (LinearLayout) mDialogView.findViewById(R.id.mydialog_buttonlist);
        mydialog_edittextlist = (LinearLayout) mDialogView.findViewById(R.id.mydialog_edittextlist);
        mydialog_custompanel = (FrameLayout) mDialogView.findViewById(R.id.mydialog_custompanel);
        setContentView(mDialogView);
        mydialog_main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                startAnimation();
            }
        });
    }

    /**
     * 获得dialogview
     * @return
     */
    public View getDialogView() {
        return mDialogView;
    }

    /**
     * 设置图标
     *
     * @param drawableResId
     * @return
     */
    public MyDialogBuilder withIcon(int drawableResId) {
        mydialog_icon.setVisibility(View.VISIBLE);
        mydialog_icon.setImageResource(drawableResId);
        return this;
    }
    public MyDialogBuilder withIcon(String imgUrl, int round) {
        mydialog_icon.setVisibility(View.VISIBLE);
//        CommonUtlis.setDisplayImageOptions(mydialog_icon,imgUrl,round,0);
        return this;
    }

    /**
     * 设置标题
     *
     * @param dialogTitle
     * @return
     */
    public MyDialogBuilder withTitie(String dialogTitle) {
        mydialog_title.setVisibility(View.VISIBLE);
        mydialog_title.setText(dialogTitle);
        return this;
    }

    /**
     * 设置内容
     *
     * @param dialogContent
     * @return
     */
    public MyDialogBuilder withContene(String dialogContent) {
        mydialog_content.setVisibility(View.VISIBLE);
        mydialog_content.setText(dialogContent);
        return this;
    }

    /**
     * 定制layout
     * @param resId 定制layout的ID
     * @return
     */
    public MyDialogBuilder setCustomView(int resId) {
        customView = View.inflate(context, resId, null);
        mydialog_custompanel.addView(customView);
        return this;
    }

    /**
     * 获得定制layout
     * @return
     */
    public View getCustomView() {
        return customView;
    }

    /**
     * 设置动画效果
     *
     * @param startEffects   开始动画
     * @param dismissEffects 结束动画
     * @return
     */
    public MyDialogBuilder withEffects(int startEffects, int dismissEffects) {
        this.startEffects = startEffects;
        this.dismissEffects = dismissEffects;
        return this;
    }

    /**
     * 动态添加按钮
     *
     * @param btnText  按钮字体
     * @param btnStyle 按钮样式
     * @param click    点击事件
     * @return
     */
    public MyDialogBuilder setBtnClick(String btnText, int btnStyle, View.OnClickListener click) {
        int textColour = 0;
        Button btn = new Button(context);
        mydialog_buttonlist.addView(btn);
        btn.setText(btnText);
        btn.setOnClickListener(click);
        // 设置按钮间距
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        lp.setMargins(40, 0, 40, 30);
        btn.setLayoutParams(lp);
        switch (btnStyle) {
            case BtnCancel:
                btn.setBackgroundResource(R.drawable.dialog_btn_cancel);
                break;
            case BtnNormal:
                btn.setBackgroundResource(R.drawable.dialog_btn_normal);
                break;
            default:
                btn.setBackgroundResource(R.drawable.dialog_btn_normal);
                break;
        }
        textColour = Color.parseColor("#ffffff");
        btn.setTextColor(textColour);
        return this;
    }

    /**
     * 动态添加输入框
     * @param etId
     * @param etHint
     * @param etInputType
     * @return
     */
    public MyDialogBuilder setEtClick(int etId, String etText, String etHint, int etInputType) {
        EditText et = new EditText(context);
        mydialog_buttonlist.addView(et);
        et.setId(100 + etId);
        et.setHint(etHint);
        et.setHintTextColor(Color.parseColor("#999999"));
        switch (etInputType) {
            case EtText:
                etInputType = InputType.TYPE_CLASS_TEXT;
                break;
            case EtPwd:
                etInputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
                break;
            case EtNum:
//                etInputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_CLASS_NUMBER;
                et.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
                break;
            case EtDecimal_Tow:
                CuttedStr_two.setPricePoint(et);
                et.setText("0.00");
                etInputType = InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER;
                break;
            default:
                break;
        }
        if (!TextUtils.isEmpty(etText)){
            et.setText(etText);
            et.setSelection(etText.length());
        }
//        et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        et.setPadding(30, 20, 30, 20);
        et.setBackgroundResource(R.drawable.dialog_et);
        et.setInputType(etInputType);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        lp.setMargins(40, 0, 40, 30);
        et.setLayoutParams(lp);
        return this;
    }

    /**
     * 单选listview
     * @param context
     * @param conten
     * @param position
     * @return
     */
    public MyDialogBuilder setListViewSingle(Context context, String[] conten, int position) {
        listviewSingle = View.inflate(context, R.layout.dialog_son_listview, null);
        mydialog_custompanel.addView(listviewSingle);
        ListView listView = (ListView) listviewSingle.findViewById(R.id.dialog_listview);
        listView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_single_choice, conten));
        listView.setItemChecked(position, true);
        listviewSingleItems = position;
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listviewSingleItems = position;
            }
        });
        setMaxHeight(listviewSingle);
        return this;
    }

    /**
     * 返回单选listview 选择位置
     * @return
     */
    public int getlistviewSingleItems() {
        return listviewSingleItems;
    }

    /**
     * 设置最大高度
     * @return
     */
    public MyDialogBuilder setMaxHeight() {
        ViewTreeObserver vto = mydialog_content.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mydialog_content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int cHeight = mydialog_content.getHeight();
                ViewGroup.LayoutParams lp = mydialog_content.getLayoutParams();
                if (cHeight >= wHeight * 1 / 2) {
                    lp.height = wHeight * 1 / 2;
                }
                mydialog_content.setLayoutParams(lp);
            }
        });
        return this;
    }

    /**
     * 设置最大高度
     * @return
     */
    public MyDialogBuilder setMaxHeight(final View view) {
        ViewTreeObserver vto = view.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                int cHeight = view.getHeight();
                ViewGroup.LayoutParams lp = view.getLayoutParams();
                if (cHeight >= wHeight * 1 / 3) {
                    lp.height = wHeight * 1 / 3;
                }
                view.setLayoutParams(lp);
            }
        });
        return this;
    }

    /**
     * 显示Dialog
     */
    @Override
    public void show() {
        super.show();
    }

    /**
     * 开始动画
     */
    private void startAnimation() {
        new BaseEffects().start(mydialog_main, startEffects);
    }

    /**
     * 消失动画
     * @param mAnimatorSet
     */
    private void dismissAnimation(BaseEffects mAnimatorSet) {
        mAnimatorSet.start(mydialog_main, dismissEffects);
    }

    /**
     * Dialog消失
     */
    @Override
    public void dismiss() {
        instance = null;
        BaseEffects mAnimatorSet = new BaseEffects();
        dismissAnimation(mAnimatorSet);
        if (mAnimatorSet.getAnimatorSet() != null) {
            // 监听动画执行
            mAnimatorSet.getAnimatorSet().addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    superDismiss();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        } else {
            superDismiss();
        }
        //隐藏键盘
        InputMethodManager imm = (InputMethodManager) this.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
        if ( imm.isActive( ) ) {
            imm.hideSoftInputFromWindow(mydialog_content.getWindowToken(), 0);
        }
    }

    /**
     * Dialog消失无动画
     */
    public void dismissNoAnimator(){
        instance = null;
        //隐藏键盘
        InputMethodManager imm = (InputMethodManager) this.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
        if ( imm.isActive( ) ) {
            imm.hideSoftInputFromWindow(mydialog_content.getWindowToken(), 0);
        }
        superDismiss();
    }

    /**
     * 继承父类消失Dialog
     */
    public void superDismiss() {
        super.dismiss();
    }

    public class BaseEffects {
        public final int DURATION = 1 * 400; // 动画执行时间
        private AnimatorSet mAnimatorSet;

        {
            mAnimatorSet = new AnimatorSet();
        }

        protected void setupAnimation(View view, int showtype) {
            switch (showtype) {
                case SlideTop:
                    mAnimatorSet = new AnimatorSet();
                    mAnimatorSet.playTogether(
                            ObjectAnimator.ofFloat(view, "translationY", -wHeight * 1 / 2, 0).setDuration(DURATION),
                            ObjectAnimator.ofFloat(view, "alpha", 0, 1).setDuration(DURATION * 3 / 2));
                    break;
                case SlideTopDismiss:
                    mAnimatorSet = new AnimatorSet();
                    mAnimatorSet.playTogether(
                            ObjectAnimator.ofFloat(view, "translationY", 0, wHeight * 1 / 2).setDuration(DURATION),
                            ObjectAnimator.ofFloat(view, "alpha", 1, 0).setDuration(DURATION));
                    break;
                default:
                    break;
            }
        }

        // 执行动画
        public void start(View view, int showtype) {
            reset(view);
            setupAnimation(view, showtype);
            mAnimatorSet.start();
        }

        public void reset(View view) {
            ViewHelper.setAlpha(view, 1);
            ViewHelper.setScaleX(view, 1);
            ViewHelper.setScaleY(view, 1);
            ViewHelper.setTranslationX(view, 0);
            ViewHelper.setTranslationY(view, 0);
            ViewHelper.setRotation(view, 0);
            ViewHelper.setRotationY(view, 0);
            ViewHelper.setRotationX(view, 0);
            ViewHelper.setPivotX(view, view.getMeasuredWidth() / 2.0f);
            ViewHelper.setPivotY(view, view.getMeasuredHeight() / 2.0f);
        }

        public AnimatorSet getAnimatorSet() {
            return mAnimatorSet;
        }
    }

    // 获得屏幕高度
    public int getScreenHeight() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    // 获得屏幕宽度
    public int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }
}

如何调用?

// TODO 如果是删除:推荐调用Adapter.notifyItemRemoved(position),不推荐Adapter.notifyDataSetChanged();
            if (menuPosition == 0) {// 撤单按钮被点击。
                final int p = adapterPosition;
                myDialogBuilder = MyDialogBuilder.getInstance(mContext);
                myDialogBuilder.withIcon(R.mipmap.iconfont_gantanhao)
                        .withContene("确定要撤单吗?")
                        .withEffects(MyDialogBuilder.SlideTop, MyDialogBuilder.SlideTopDismiss)
                        .setBtnClick("确定", MyDialogBuilder.BtnNormal, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialogBuilder.dismissNoAnimator();

                                deleteOddNum = list.get(p)[0];//商品订单号

                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                Date curDate = new Date(System.currentTimeMillis());
                                String currentTime = formatter.format(curDate);//获取手机系统时间是个漏洞,用户可手动更改时间。

                                requestHandleArrayList.add(requestAction.rongyunRevoke(Todayearning1Activity.this, deleteOddNum, currentTime));

                            }
                        })
                        .setBtnClick("取消", MyDialogBuilder.BtnCancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialogBuilder.dismiss();
                            }
                        })
                        .show();
            }
时间: 2024-11-09 01:02:07

弹框DialogBuilder的使用的相关文章

layer弹框的上面各个属性 -可配置

<script type="text/javascript"> 12 //eg 13 layer.open({ 14 title:"标题信息提示",//标题信息 15 content: 'test',//内容区域 16 move: false,//是否可以拖动,默认可以拖动 17 btn: ['按钮一', '按钮二', '按钮三'], 18 btn1: function(index){ 19 //按钮[按钮一]的回调 20 layer.close(ind

js弹框3秒后自动消失

开发中有时候会需要最出弹框后,过几秒自动消失的效果,下面给大家分享一下我自己做的一个小案例. 案例中的弹框使用的是bootstrap里面的模态框,实现自动消失则用的是js中的setInterval方法.该弹框使用了jquery-ui中的draggable方法,可拖动. 目录结构如下: 下面是案例代码: demo.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti

弹框提示用户输入

在很多页面,都会有提示用户输入账号和密码的弹框.并保障用户的良好体验效果. <html>   <head>   <title></title>   <meta http-equiv="content" content="text/html" charset="utf-8"/>   <style type="text/css">   body{   back

3种常用IOS弹框

目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButton

android如果给TextView或EditText的email链接加下划线,并在点击在email连接上可以弹框显示

如何把textview的一些文字加上背景色: Spannable str = new SpannableString("#fdsfdfsdfdsfd#"); Matcher matcher = getEmailPattern().matcher((CharSequence) str); while (matcher.find()) { int start = matcher.start(); int end = matcher.end(); str.setSpan(new Foregr

未加域客户端使用Lync时反复弹框要求输入凭证

最近发现部分未加域客户端打开Lync 2010时反复弹框要求输入凭证,且提示与Exchange进行连接时凭证错误,此时感觉问题原因因该是发生在两端客户端信息不匹配导致的,此时在Lync客户端帐号登录处输入完整域帐号(帐号@域名)后,并记住密码后无任何报错信息,且退出重新登录也不再提示,outlook相关日历信息也得到了正常同步. 未加域客户端使用Lync时反复弹框要求输入凭证,布布扣,bubuko.com

如果exclipe中Java视图中Consol跑偏了单独形成一个弹框怎么办?

问题如图: 不和其他的在同一窗体中,而且拖动也拖不进去,则是使用以下方法: 点击工具栏上的Window--->点击Reset Perspective...这样就可以解决了. 如果exclipe中Java视图中Consol跑偏了单独形成一个弹框怎么办?,布布扣,bubuko.com

vue弹框,删除元素

1.效果 2.index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0"> <title>Ti

Jquery学习笔记(8)--京东导航菜单(2)增加弹框

京东导航,添加中间的弹框栏,使用position定位,放在左侧栏的li标签里面,成为一个整体,保证鼠标在弹框里的时候,弹框不消失: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <script src="jquery.js&quo