前言
平时项目开发总要自定义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:408030208@qq.com
*/
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:408030208@qq.com
*/
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
<style name="dialog_ios_style" parent="@android:style/Theme.Dialog">
<!--window 背景,这里可以设置整个窗体的背景,不过会被窗体里面的内容布局覆盖,圆角,窗体大小在某种情况下可以通过这里的背景设置-->
<item name="android:windowBackground">@drawable/yuanjiao_background</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowMinWidthMajor">100%</item>
</style>
yuanjiao_background.xml 窗体背景布局xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<size android:height="200dp" android:width="200dp"/>
<corners android:radius="20dp"/>
<solid android:color="@android:color/white" />
</shape>