自定义Dialog样式

前言

平时项目开发总要自定义dialog满足产品的设计需求,但系统提供Dialog和AlertDialog用起来不是很方便,所以自己封装一个好用的Dialog基类是再好不过了。

实现

BaseDialog.java

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.WindowManager;
/**
 * Created by WJX .
 * Desc: 一个便于自定义Dialog样式的基类
 * Created on 2017/1/7 11:19.
 * Mail:[email protected]
 */
public abstract class BaseDialog {
    protected Context context;
    private Dialog dialog;
    protected abstract int getDialogStyleId();//子类实现获取样式id
    protected abstract View getView();//子类实现内容布局View
    protected BaseDialog(Context context){
        this.context=context;
        //初始化基础对话框
        if (getDialogStyleId()==0){
            dialog=new Dialog(context);
        }else {
            dialog=new Dialog(context,getDialogStyleId());
        }
        //dialog设置内容布局view
        dialog.setContentView(getView());
        //关闭系统键盘
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    }
    /**
     * dialog设置能否点击取消,链式
     */
public BaseDialog setCancelable(boolean cacel){
    dialog.setCancelable(cacel);
    return this;
}
    /**
     * dialog显示,链式
     */
    public BaseDialog show(){
        dialog.show();
        return this;
    }
    /**
     * dialog消失,链式
     */
    public BaseDialog  disMiss(){
        dialog.dismiss();
        return this;
    }
    /**
     * dialog是否在显示,链式
     */
    public boolean isShowing(){
        return dialog.isShowing();
    }
    /**
     * dialog消失监听,链式
     */
    public BaseDialog setOnDismissListener(DialogInterface.OnDismissListener dismissListener){
    dialog.setOnDismissListener(dismissListener);
        return this;
    }
    /**
     * dialog取消监听,链式
     */
    public BaseDialog setOnCancelListener(DialogInterface.OnCancelListener cancelListener){
        dialog.setOnCancelListener(cancelListener);
        return this;
    }
    /**
     * dialog显示监听,链式
     */
    public BaseDialog setOnShowListener(DialogInterface.OnShowListener onShowListener){
     dialog.setOnShowListener(onShowListener);
        return this;
    }
}

YuanJiaoDialog.java 圆角对话框

/**
 * Created by WJX .
 * Desc: 一个圆角dialog
 * Created on 2017/1/7 11:05.
 * Mail:[email protected]
 */
public class YuanJiaoDialog extends BaseDialog {
    public YuanJiaoDialog(Context context) {
        super(context);
    }
    @Override
    protected int getDialogStyleId() {
        return R.style.dialog_style_one;
    }
    @Override
    protected View getView() {
        View view= LayoutInflater.from(context).inflate(R.layout.dialog_conent_layout,null);
        return view;
    }
}

dialog_style_one


yuanjiao_background.xml 窗体背景布局xml



    
    
    

你可能感兴趣的:(自定义Dialog样式)