分享

Android之Material Dialogs详解(非原创)

 新进小设计 2022-01-17

文章大纲

一、什么是Material Dialogs
二、Material Dialogs实战
三、项目源码下载

一、什么是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();

运行结果如下:

温馨提示:取消弹框采用dialog.dismiss();

3. 创建带图标弹框(包含两个按钮)

new MaterialDialog.Builder(MainActivity.this)                        .title("标题")                        .content("内容;测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度测试长度")                        .positiveText("确认")                        .negativeText("取消")                        .icon(getResources().getDrawable(R.drawable.ic_launcher))                        .show();

运行结果如下:

温馨提示:可以用maxIconSize(int size)限制图标最大size

4. 创建带三个按钮弹框

new MaterialDialog.Builder(MainActivity.this)                        .title("标题")                        .content("内容")                        .positiveText("确认")                        .negativeText("取消")                        .neutralText("更多")                        .show();

运行结果如下:

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();
            }
        });

运行结果如下:

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();

运行结果如下:

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();

运行结果如下:

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();

运行结果如下:

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();

运行结果如下:

10. 创建自定义视图

new MaterialDialog.Builder(MainActivity.this)                        .title("Title")                        .customView(R.layout.activity_main, true)                        .positiveText("CHOOSE")                        .show();

运行结果如下:

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();

运行结果如下:

12. 创建等待提示框(圆圈加载)

new MaterialDialog.Builder(MainActivity.this)                        .title("提示框")                        .content("请稍等...")                        .progress(true, 0)                        .show();

运行结果如下:

13. 创建等待提示框(水平加载)

new MaterialDialog.Builder(MainActivity.this)                        .title("提示框")                        .content("请稍等...")                        .progress(true, 0)                        .progressIndeterminateStyle(true)                        .show();

运行结果如下:

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();

运行结果如下:

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)!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约