文章目录
- DialogX
- 一、引入
- 二、基础对话框 MessageDialog 和 输入对话框 InputDialog
- 2.1.0 显示一个简单对话框
- 2.1.1 构造对话框
- 2.1.2 按钮点击回调
- 2.2 输入对话框按钮点击回调
- 2.3自定义布局
- 2.4自定义进入和关闭动画
- 三、等待框WaitDialog和提示框TipDialog
- 3.1 等待框
- 3.2 提示框
- 四、底部对话框 BottomDialog
- 五、全屏对话框FullScreenDialog
- 六、自定义对话框CustomDialog
- 七、引导对话框GuideDialog
dialogX视频讲解
DialogX
一款简单易用的对话框组件,相比原生对话框使用体验更佳,可自定义程度更高,扩展性更强,轻松实现各种对话框、菜单和提示效果,更有iOS、MIUI、Material You等主题扩展可选。
一、引入
1、下载地址:https://github.com/kongzue/DialogX/releases
- 下载aar包
- 将aar包放入项目的libs目录下
- 进入app下的build.gradle的dependencies{}中加上下列代码
implementation (fileTree("libs"))
- 点击Sync now完成同步
二、基础对话框 MessageDialog 和 输入对话框 InputDialog
基础对话框组件可以实现基本的对话框业务逻辑,包含标题、消息文本、单/双/三按钮的提醒功能,三个按钮可以按照纵向/横向进行显示,满足绝大部分日常阻断式提醒需求。
输入对话框 InputDialog 是基础对话框的扩展组件,除了包含基础的功能外还提供了输入框,可自定义输入提示文本、输入文字样式和点击按钮后的输入内容回调等。
2.1.0 显示一个简单对话框
使用以下代码显示一个对话框:
MessageDialog.show("标题", "正文内容", "确定", "取消", "其他");
标题、正文内容都可以传入 null,则不会显示相应的文本布局。
MessageDialog.show(null,null,"确定","取消","其他")
若需要将按钮显示为纵向的,可以通过以下代码设置:
MessageDialog messageDialog = new MessageDialog("标题", "正文内容", "确定", "取消", "其他").setButtonOrientation(LinearLayout.VERTICAL);
messageDialog.show();
2.1.1 构造对话框
通过上述代码可以看到,对话框组件可以通过 .show(...)
静态方法直接创建并显示,也可以通过new
进行创建,另外要创建一个空的对话框,可以使用 MessageDialog.build()
来实例化对话框对象。
若在项目中引入了多个主题,需要临时改变某一个对话框的主题,亦或者是需要临时改变对话框的亮色和暗色模式,那么必须使用非“show”的方法创建,修改主题或颜色后再显示,方法如下:
MessageDialog.build().setStyle(IOSStyle.style()).setTheme(DialogX.THEME.DARK).setTitle("标题").setMessage("内容").setOkButton("确定").show();
除此之外,也可以使用 new 指令来构建 MessageDialog 和 InputDialog,DialogX 的对话框支持多种构建方式,随你所想,随你所用。
new MessageDialog().setTitle("标题").setMessage("内容").setOkButton("确定").show();
2.1.2 按钮点击回调
MessageDialog.show("退出", "您确定要退出应用程序吗", "确定").setOkButton(new OnDialogButtonClickListener<MessageDialog>() {@Overridepublic boolean onClick(MessageDialog baseDialog, View v) {System.exit(0);return false;}
});
回调中有一个返回值,若return true
则可以点击后不自动关闭对话框。
同时,也可以设置按钮文本并设置回调
.setOkButton("确定", new OnDialogButtonClickListener<MessageDialog>() {
}
2.2 输入对话框按钮点击回调
InputDialog 的回调与 MessageDialog 的回调略有不同,在回调参数中个给出了输入文本内容,方便直接判断和获取输入的文本:
new InputDialog("标题", "正文内容", "确定", "取消", "").setCancelable(false).setOkButton(new OnInputDialogButtonClickListener<InputDialog>() {@Overridepublic boolean onClick(InputDialog baseDialog, View v, String inputStr) {System.out.println(inputStr);return false;}}).show();
另外也可以使用 inputDialog.getInputText()
来获取输入的文本内容。
2.3自定义布局
btnMainCustom.setOnClickListener(view -> {MessageDialog.build().show().setCustomView(new OnBindView<MessageDialog>(R.layout.custom_layout) {@Overridepublic void onBind(MessageDialog dialog, View v) {Button btnExit = v.findViewById(R.id.btn_exit);btnExit.setOnClickListener(view -> {System.exit(0);});Button btnCancel = v.findViewById(R.id.btn_cancel);btnCancel.setOnClickListener(view -> {dialog.dismiss();});}});});
2.4自定义进入和关闭动画
针对单次显示的对话框动画修改
.setAnimResId(com.kongzue.dialogx.R.anim.anim_dialogx_bottom_enter, com.kongzue.dialogx.R.anim.anim_dialogx_bottom_exit)
另外你也可以通过 setEnterAnimDuration([long])
设置入场动画时长以及通过 .setExitAnimDuration([long])
设置关闭动画时长。
还可以通过静态属性直接修改全局MessageDialog、InputDialog的动画
//设置全局 MessageDialog 入场动画
MessageDialog.overrideEnterAnimRes = com.kongzue.dialogx.R.anim.anim_dialogx_bottom_enter;
//设置全局 MessageDialog 出场动画
MessageDialog.overrideExitAnimRes = com.kongzue.dialogx.R.anim.anim_dialogx_bottom_exit;
//设置全局 MessageDialog 入场动画时间
MessageDialog.overrideEnterDuration = 1000;
//设置全局 MessageDialog 出场动画时间
MessageDialog.overrideExitDuration = 1000;
三、等待框WaitDialog和提示框TipDialog
3.1 等待框
WaitDialog.show("Please Wait!");
等待框 WaitDialog 和提示框 TipDialog的背景是与亮/暗色模式相反的,这是为了突出显示。
也可以注册监听器,实现按下返回键退出对话框
btnWait.setOnClickListener(view -> {WaitDialog.show("Please Wait!").setOnBackPressedListener(new OnBackPressedListener<WaitDialog>() {@Overridepublic boolean onBackPressed(WaitDialog dialog) {dialog.doDismiss();return false;}});});
3.2 提示框
完成提示框
TipDialog.show("Success!", WaitDialog.TYPE.SUCCESS);
警告提示框
TipDialog.show("Warning!", WaitDialog.TYPE.WARNING);
错误提示框
TipDialog.show("Success!", WaitDialog.TYPE.SUCCESS, 3500);
我们通过多线程实现等待三秒后提示完成,同时注册返回键监听器
btnWait.setOnClickListener(view -> {WaitDialog show = WaitDialog.show("Please Wait!");WaitDialogHandleThread waitDialogHandleThread = new WaitDialogHandleThread();Thread thread = new Thread(waitDialogHandleThread);thread.start();show.setOnBackPressedListener(new OnBackPressedListener<WaitDialog>() {@Overridepublic boolean onBackPressed(WaitDialog dialog) {dialog.doDismiss();return false;}});});
public class WaitDialogHandleThread implements Runnable{@Overridepublic void run() {try {Thread.sleep(5000);TipDialog.show("Success", WaitDialog.TYPE.SUCCESS);} catch (InterruptedException e) {throw new RuntimeException(e);}}
}
四、底部对话框 BottomDialog
显示一个最简单的BottomDialog
BottomDialog.show("标题","对话框内容");
也可以自定义布局
btnSimpleButton.setOnClickListener(view -> {BottomDialog.build().setCustomView(new OnBindView<BottomDialog>(R.layout.custom_layout) {@Overridepublic void onBind(BottomDialog dialog, View v) {Button btnExit = v.findViewById(R.id.btn_exit);btnExit.setOnClickListener(view -> {System.exit(0);});Button btnCancel = v.findViewById(R.id.btn_cancel);btnCancel.setOnClickListener(view -> {dialog.dismiss();});}}).show();});
五、全屏对话框FullScreenDialog
全屏对话框静态方法show()通过new OnBindView传入自定义布局
btnFullScreen.setOnClickListener(view -> {FullScreenDialog.show(new OnBindView<FullScreenDialog>(R.layout.custom_layout) {@Overridepublic void onBind(FullScreenDialog dialog, View v) {Button btnExit = v.findViewById(R.id.btn_exit);btnExit.setOnClickListener(view -> {System.exit(0);});Button btnCancel = v.findViewById(R.id.btn_cancel);btnCancel.setOnClickListener(view -> {dialog.dismiss();});}});});
六、自定义对话框CustomDialog
通过静态方法show()构建
CustomDialog.show(new OnBindView<CustomDialog>(R.layout.custom_layout) {@Overridepublic void onBind(CustomDialog dialog, View v) {Button btnExit = v.findViewById(R.id.btn_exit);btnExit.setOnClickListener(view -> {System.exit(0);});Button btnCancel = v.findViewById(R.id.btn_cancel);btnCancel.setOnClickListener(view -> {dialog.dismiss();});}});
显示对话框的位置,可以指定一个界面上已存在的 view,使 CustomDialog 围绕其位置显示
.setAlignBaseViewGravity(btnCustom,Gravity.BOTTOM);
CustomDialog 默认不实现对话框背景遮罩,这是为了丰富扩展性。如果需要背景遮罩,您可以自行使用如下代码设置:
customDialog.setMaskColor(colorInt);
七、引导对话框GuideDialog
通过.build().setCustomView(new OnBindView)绑定布局
通过.setAlignBaseViewGravity绑定控件对象
通过.setStageLightType设置引导效果
STAGE_LIGHT_TYPE 类型 | 介绍 |
---|---|
RECTANGLE | 基于绑定布局外围显示矩形舞台光 |
SQUARE_OUTSIDE | 基于绑定布局外围显示正方形舞台光 |
SQUARE_INSIDE | 基于绑定布局内围显示正方形舞台光 |
CIRCLE_OUTSIDE | 基于绑定布局外围显示圆形舞台光 |
CIRCLE_INSIDE | 基于绑定布局内围显示圆形舞台光 |