Android之Material Dialogs详解(非原创)
文章大纲
一、什么是Material Dialogs
二、Material Dialogs实战
三、项目源码下载
![](https://upload-images.jianshu.io/upload_images/16598307-0ee5e6b26efbf063.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/842/format/webp)
一、什么是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://upload-images.jianshu.io/upload_images/16598307-42ba324dd189dbcf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/837/format/webp)
温馨提示:取消弹框采用dialog.dismiss();
3. 创建带图标弹框(包含两个按钮)
new MaterialDialog.Builder(MainActivity.this)
.title("标题")
.content("内容;测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度")
.positiveText("确认")
.negativeText("取消")
.icon(getResources().getDrawable(R.drawable.ic_launcher))
.show();
运行结果如下:
![](https://upload-images.jianshu.io/upload_images/16598307-1901642a4491a44d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/818/format/webp)
温馨提示:可以用maxIconSize(int size)限制图标最大size
4. 创建带三个按钮弹框
new MaterialDialog.Builder(MainActivity.this)
.title("标题")
.content("内容")
.positiveText("确认")
.negativeText("取消")
.neutralText("更多")
.show();
运行结果如下:
![](https://upload-images.jianshu.io/upload_images/16598307-521afb9f1bded044.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/812/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-763988e6e9c05e8b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/837/format/webp)
![](https://upload-images.jianshu.io/upload_images/16598307-dff92fe68656f790.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/837/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-1f8de617a59c467a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/802/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-cbd418e7cfd5ba0e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/770/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-7eed12ef3a479921.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/521/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-c7ffece199051a40.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/782/format/webp)
10. 创建自定义视图
new MaterialDialog.Builder(MainActivity.this)
.title("Title")
.customView(R.layout.activity_main, true)
.positiveText("CHOOSE")
.show();
运行结果如下:
![](https://upload-images.jianshu.io/upload_images/16598307-e385e99edbb38abb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/480/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-36f6e67a54b64bb3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/554/format/webp)
12. 创建等待提示框(圆圈加载)
new MaterialDialog.Builder(MainActivity.this)
.title("提示框")
.content("请稍等...")
.progress(true, 0)
.show();
运行结果如下:
![](https://upload-images.jianshu.io/upload_images/16598307-632bf1277615d341.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/513/format/webp)
13. 创建等待提示框(水平加载)
new MaterialDialog.Builder(MainActivity.this)
.title("提示框")
.content("请稍等...")
.progress(true, 0)
.progressIndeterminateStyle(true)
.show();
运行结果如下:
![](https://upload-images.jianshu.io/upload_images/16598307-0c746e949e870545.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/485/format/webp)
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://upload-images.jianshu.io/upload_images/16598307-51391ad30dea303e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/514/format/webp)
![](https://upload-images.jianshu.io/upload_images/16598307-9e6201d4604fc7e7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/472/format/webp)
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)!
三、项目源码下载
I hate being alone,but I'm afraid I won't get married early