文章大纲
一、什么是Material Dialogs
二、Material Dialogs实战
三、项目源码下载
![](https://i-blog.csdnimg.cn/blog_migrate/510582d65744aaa08f41f05090b7f12b.webp?x-image-process=image/format,png)
一、什么是Material Dialogs
Material Dialogs是一个漂亮、流畅、可定制的对话框,核心模块包含创建基本、列表、单/多选项、进度、输入等对话框。
二、Material Dialogs实战
使用注意点:
(1)使用MaterialDialog的Activity需要继承AppCompat
(2)默认的MaterialDialog在点击空白位置时候会自动关闭弹出框,取消dialog以外的区域的点击事件代码如下
//取消dialog以外的区域的点击事件(默认为true)这里我们设置成false就ok啦
.canceledOnTouchOutside(false)
1. 添加依赖
//添加material-dialogs依赖
//核心模块的依赖
implementation 'com.afollestad.material-dialogs:core:0.9.1.0'
//公共模块的依赖 //公共模块包含不是每个人都需要的扩展库。 这包括 ColorChooserDialog、FolderChooserDialog、Material Preference 类和 MaterialSimpleListAdapter / MaterialSimpleListItem implementation 'com.afollestad.material-dialogs:commons:0.9.1.0'
2. 创建基本弹框
new MaterialDialog.Builder(MainActivity.this) .title("标题") .content("内容") .positiveText("确认") .negativeText("取消") .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/103cd44eeb05ffa18cd707ed1af4da06.webp?x-image-process=image/format,png)
温馨提示:取消弹框采用dialog.dismiss();
3. 创建带图标弹框(包含两个按钮)
new MaterialDialog.Builder(MainActivity.this) .title("标题") .content("内容;测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度") .positiveText("确认") .negativeText("取消") .icon(getResources().getDrawable(R.drawable.ic_launcher)) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8d6833aed769af88ca83e6b179bf245c.webp?x-image-process=image/format,png)
温馨提示:可以用maxIconSize(int size)限制图标最大size
4. 创建带三个按钮弹框
new MaterialDialog.Builder(MainActivity.this) .title("标题") .content("内容") .positiveText("确认") .negativeText("取消") .neutralText("更多") .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/f1d2d6ba06960d83f58ef0beb7edbc75.webp?x-image-process=image/format,png)
5. 创建带点击事件的弹出框
//如果你想监听三种动作按钮,只用一个onAny就行了,which会告诉你点击了什么按钮,根据需要去判断,which.toString()的名字分别是Positive,Negative等
//new MaterialDialog.Builder(this)
// .title("标题")
// .content("内容") // .positiveText("确认") // .negativeText("取消") // .neutralText("更多") // .onAny(new MaterialDialog.SingleButtonCallback() { // @Override // public void onClick(MaterialDialog dialog, DialogAction which) { // Toast.makeText(MainActivity.this, which.toString(), Toast.LENGTH_SHORT).show(); // } // }) // .show(); click_button = (Button)findViewById(R.id.click_button); click_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new MaterialDialog.Builder(MainActivity.this) .title("标题") .content("内容") .positiveText("确认") .negativeText("取消") .neutralText("更多") .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { Toast.makeText(MainActivity.this, "点击了确认按钮", Toast.LENGTH_SHORT).show(); } }) .onNeutral(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { Toast.makeText(MainActivity.this, "点击了更多按钮", Toast.LENGTH_SHORT).show(); } }) .onNegative(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show(); } }) .show(); } });
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/9d248035ca6ec4edc6432d2862d6e759.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/6198b116e3655edb836647c6ed4dded4.webp?x-image-process=image/format,png)
6. 创建是否不再询问弹出框
new MaterialDialog.Builder(MainActivity.this)
.title("标题")
.content("内容") .positiveText("确认") //第二次参数代表初始状态是否为勾选 .checkBoxPromptRes(R.string.isShowAgain, true, null) .onAny(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(MaterialDialog dialog, DialogAction which) { //dialog.isPromptCheckBoxChecked()+""如果返回true,表示用户勾选了,false表示没有 Toast.makeText(MainActivity.this, dialog.isPromptCheckBoxChecked()+"", Toast.LENGTH_SHORT).show(); } }) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/0953dfb18cbe10fc986847b1e88288a9.webp?x-image-process=image/format,png)
7. 创建不带样式列表单选框
final String[] array = {"aa","bb"};
new MaterialDialog.Builder(MainActivity.this) .title("标题") .positiveText("确认") .negativeText("取消") .items(array) .itemsCallback(new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { Toast.makeText(MainActivity.this, which+"", Toast.LENGTH_SHORT).show(); } }) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/243eee0e582e3f4cf07a8de3233e42f4.webp?x-image-process=image/format,png)
8. 创建带样式的列表单选框
new MaterialDialog.Builder(MainActivity.this)
.title("标题")
.items(array)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() { @Override public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) { return true; } }) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/700309b8ee00618e946f98fe96a795a3.webp?x-image-process=image/format,png)
9. 创建多选列表
new MaterialDialog.Builder(MainActivity.this)
.title("标题")
.positiveText("确认") .items(array) .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() { @Override public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) { return true; } }) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/6bf5ff7631aa66f0d18718e9fcd56707.webp?x-image-process=image/format,png)
10. 创建自定义视图
new MaterialDialog.Builder(MainActivity.this) .title("Title") .customView(R.layout.activity_main, true) .positiveText("CHOOSE") .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8e2ca6890d3f958e2daec75366c1819f.webp?x-image-process=image/format,png)
11.创建带限制的输入框
new MaterialDialog.Builder(MainActivity.this)
.title("Title")
//限制输入的长度 .inputRangeRes(2, 20, R.color.colorPrimary) //限制输入类型 .inputType(InputType.TYPE_CLASS_PHONE) .input("哈哈哈", null, new MaterialDialog.InputCallback() { @Override public void onInput(MaterialDialog dialog, CharSequence input) { Toast.makeText(MainActivity.this, input, Toast.LENGTH_SHORT).show(); } }) .positiveText("确定") .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/32cd514956544a16154d4600c1e62053.webp?x-image-process=image/format,png)
12. 创建等待提示框(圆圈加载)
new MaterialDialog.Builder(MainActivity.this) .title("提示框") .content("请稍等...") .progress(true, 0) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/88350093b648c5952d2f86cba04f4e1c.webp?x-image-process=image/format,png)
13. 创建等待提示框(水平加载)
new MaterialDialog.Builder(MainActivity.this) .title("提示框") .content("请稍等...") .progress(true, 0) .progressIndeterminateStyle(true) .show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/17df313351f3976da1dd2ba73b71698b.webp?x-image-process=image/format,png)
14. 创建带进度条的等待框
new MaterialDialog.Builder(MainActivity.this).title("Progress").content("please wait...").contentGravity(GravityEnum.CENTER).progress(false, 100, true).cancelListener(dialog -> { if (thread != null) { thread.interrupt(); } }).showListener(dialogInterface -> { final MaterialDialog dialog = (MaterialDialog) dialogInterface; startThread(() -> { while (dialog.getCurrentProgress() != dialog.getMaxProgress() && !Thread.currentThread().isInterrupted()) { if (dialog.isCancelled()) { break; } try { Thread.sleep(50); } catch (InterruptedException e) { break; } dialog.incrementProgress(1); } runOnUiThread(() -> { thread = null; dialog.setContent(getString(R.string.md_done_label)); }); }); }).show();
运行结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/fa8f92fee05076107ca20057bb595a4f.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/378397afd93feb4c378c853ff257e934.webp?x-image-process=image/format,png)
15.其他功能
(1)MaterialDialog可以通过以下方式设置颜色
// .titleColorRes(R.color.material_red_500)
// .contentColor(Color.WHITE) // notice no 'res' postfix for literal color
// .linkColorAttr(R.attr.my_link_color_attr) // notice attr is used instead of none or res for attribute resolving
// .dividerColorRes(R.color.material_pink_500) // .backgroundColorRes(R.color.material_blue_grey_800) // .positiveColorRes(R.color.material_red_500) // .neutralColorRes(R.color.material_red_500) // .negativeColorRes(R.color.material_red_500) // .widgetColorRes(R.color.material_red_500) // .buttonRippleColorRes(R.color.material_red_500)
(2)通过以下方式设置MaterialDialog主题
.theme(Theme.DARK)
(3)设置文字、按钮等内容的位置
.titleGravity(GravityEnum.CENTER)
.contentGravity(GravityEnum.CENTER) .btnStackedGravity(GravityEnum.START) .itemsGravity(GravityEnum.END) .buttonsGravity(GravityEnum.END)
(4)改变drawable被点击或聚焦时的状态
.btnSelector(R.drawable.custom_btn_selector) .btnSelector(R.drawable.custom_btn_selector_primary, DialogAction.POSITIVE) .btnSelectorStacked(R.drawable.custom_btn_selector_stacked) .listSelector(R.drawable.custom_list_and_stackedbtn_selector)!