欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个简单的自定义Dialog

程序员文章站 2022-07-02 19:18:32
...

在平时的开发中,我们可能时常需要自定义Dialog,比如常见的需求,通知提示用户的dialog,第二种是从底部弹出的dialog,第三种是带输入框的dialog等。下面就是对一个自定义dialog的简单的封装。

直接看效果,不会作图,图都是网上找的。

一个简单的自定义Dialog

1,看普通的提示框效果,我只是展示效果,界面并不是很详细

一个简单的自定义Dialog

2,从底部弹出效果

一个简单的自定义Dialog
3,带输入控件的弹出效果,自动启动输入法

一个简单的自定义Dialog

下面时自定义的CustomDialog类:

/**
 * Created by wjq on 2017/5/16.
 */

public class CustomDialog extends Dialog{

    private Context context;
    private int height, width;
    private boolean cancelTouchout;
    private View view;
    private int mLocation; //dialog从哪里弹出
    private int dialogAnim; //dialog显示隐藏动画


    private CustomDialog(Builder builder) {
        super(builder.context);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        mLocation = builder.location;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }


    private CustomDialog(Builder builder, int resStyle) {
        super(builder.context, resStyle);
        context = builder.context;
        height = builder.height;
        width = builder.width;
        mLocation = builder.location;
        cancelTouchout = builder.cancelTouchout;
        view = builder.view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(view);

        setCanceledOnTouchOutside(cancelTouchout);

        Window win = getWindow();
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.gravity = mLocation;
        lp.height = height;
        lp.width = width;
        win.setAttributes(lp);
    }

    public static final class Builder {

        private Context context;
        private int height, width;
        private boolean cancelTouchout;
        private View view;
        private int resStyle = -1;  //dialog显示样式文件
        private int location = Gravity.CENTER; //默认从中间弹出
        private int animStyle = -1;   //dialog进入及关闭时动画的样式文件


        public Builder(Context context) {
            this.context = context;
        }

        public Builder view(int resView) {
            view = LayoutInflater.from(context).inflate(resView, null);
            return this;
        }

        public Builder heightpx(int val) {
            height = val;
            return this;
        }

        public Builder widthpx(int val) {
            width = val;
            return this;
        }

        public Builder heightdp(int val) {
            height = DensityUtil.dip2px(context, val);
            return this;
        }

        public Builder widthdp(int val) {
            width = DensityUtil.dip2px(context, val);
            return this;
        }

        public Builder heightDimenRes(int dimenRes) {
            height = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder widthDimenRes(int dimenRes) {
            width = context.getResources().getDimensionPixelOffset(dimenRes);
            return this;
        }

        public Builder style(int resStyle) {
            this.resStyle = resStyle;
            return this;
        }

        public Builder dialogStyle(){
            this.animStyle = resStyle;
            return this;
        }

        public Builder location(int location){
            this.location = location;
            return this;
        }

        public Builder cancelTouchout(boolean val) {
            cancelTouchout = val;
            return this;
        }

        public Builder addViewOnclick(int viewRes,View.OnClickListener listener){
            view.findViewById(viewRes).setOnClickListener(listener);
            return this;
        }


        public CustomDialog build() {
            if (resStyle != -1) {
                return new CustomDialog(this, resStyle);
            } else {
                return new CustomDialog(this);
            }
        }
    }
}

当没有输入控件时Dialog样式

    <style name="Dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@color/full_transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
    </style>

当有输入控件时Dialog样式

    <style name="inputDialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowBackground">@color/full_transparent</item>
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:backgroundDimEnabled">true</item>
        <!--下面这个是针对带输入内容的dialog时设置输入法自动弹出-->
        <item name="android:windowSoftInputMode">adjustResize|stateAlwaysVisible</item>
    </style>

整个使用该自定义Dialog的类如下:

/***
 * create by wjq
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_dialog_one;
    Button btn_dialog_two;
    Button btn_dialog_three;
    CustomDialog dialog_one;
    CustomDialog dialog_two;
    CustomDialog dialog_three;
    CustomDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_dialog_one = (Button) findViewById(R.id.btn_dialog_one);
        btn_dialog_two = (Button) findViewById(R.id.btn_dialog_two);
        btn_dialog_three = (Button) findViewById(R.id.btn_dialog_three);
        btn_dialog_one.setOnClickListener(this);
        btn_dialog_two.setOnClickListener(this);
        btn_dialog_three.setOnClickListener(this);
        builder = new CustomDialog.Builder(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog_one:
                showDialogOne();
                break;
            case R.id.btn_dialog_two:
                showDialogtwo();
                break;
            case R.id.btn_dialog_three:
                showDialogthree();
                break;
        }
    }

    private void showDialogthree() {
        dialog_three = builder.style(R.style.inputDialog)
                .heightDimenRes(R.dimen.dialog_height)
                .widthDimenRes(R.dimen.dialog_width)
                .cancelTouchout(true)
                .view(R.layout.dialog_three)
                .build();
        dialog_three.show();
    }

    private void showDialogOne() {
        dialog_one = builder.style(R.style.Dialog)  //Dialog的样式
                .heightDimenRes(R.dimen.dialog_height) //Dialog高
                .widthDimenRes(R.dimen.dialog_width)   //Dialog宽
                .cancelTouchout(true)           //点击Dialog外面,Dialog是否关闭
                .view(R.layout.dialog_one)      //Dialog布局文件
                .build();
        dialog_one.show();
    }

    private void showDialogtwo() {
        dialog_one = builder.style(R.style.Dialog)
                .heightDimenRes(R.dimen.dialog_height)
                .widthDimenRes(R.dimen.dialog_width)
                .location(Gravity.BOTTOM)
                .cancelTouchout(true)
                .view(R.layout.dialog_two)
                .build();
        dialog_one.show();
    }
}

上面就是对Dialog的简单封装。完整demo下载DialogDemo