Android 开发 MaterialDialog框架的详解

前言

  开始之前还是需要废话一下,因为有一些坑需要告知。首先MaterialDialog在GitHub上作者已经转型使用100% Kotlin语言编写,虽然可以在Java里调用Kotlin使用。但是个人暂时不想接触,所以依然会使用老版本的MaterialDialog。Java最后的版本是0.9.6.0版本,所以我们以这个版本为例子记录一些平时个人用到的例子。另外不需要太担心高版本无法适配,目前在Android 9.0版本依然效果良好。  

  作者的GitHub地址:https://github.com/afollestad/material-dialogs

  使用参考博客:https://blog.csdn.net/u010904027/article/details/53535590

依赖

  implementation 'com.afollestad.material-dialogs:core:0.9.6.0'

代码例子

 

等待弹窗例子

/**
     * 等待弹窗1
     */
    public void waitFor1() {
        MaterialDialog waitForDialog = new MaterialDialog.Builder(this)
                .content("正在登入...")
                .progress(true,-1)//等待图标 true=圆形icon false=进度条
                .canceledOnTouchOutside(false)//点击外部不取消对话框
                .build();
        waitForDialog.show();
    }

    /**
     * 等待弹窗2
     */
    @Override
    public void waitFor2() {
        MaterialDialog waitForDialog = new MaterialDialog.Builder(this)
                .content("正在登入...")
                .progress(true,-1)//等待图标 true=圆形icon false=进度条
                .cancelable(false)//不会被取消 (包括返回键和外部点击都无法取消)
                .build();
        waitForDialog.show();
    }

普通对话框例子

public void dialog() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .title("提示")//标题
                .content("您未添加人脸识别,请点击确定录入")//内容
                .icon(getResources().getDrawable(R.mipmap.ic_logo,null))//图标
                .positiveText("确定") //肯定按键
                .neutralText("稍后询问")  //中性按键
                .negativeText("取消") //否定按键
                .cancelable(true)
                .onPositive(new MaterialDialog.SingleButtonCallback() { //监听肯定按键
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    }
                })
                .onNeutral(new MaterialDialog.SingleButtonCallback() { //监听中性按键
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() { //监听否定按键
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                    }
                })
                .onAny(new MaterialDialog.SingleButtonCallback() {//三个按键一起监听
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        switch (which){
                            case POSITIVE:
                                break;
                            case NEUTRAL:
                                break;
                            case NEGATIVE:
                                break;
                        }
                        //或者这样
//                            if (DialogAction.POSITIVE == which){
//
//                            }
                    }
                })
                .build();
        dialog.show();
    }

 单选列表对话框例子

private void singleElectionDialog(){
        int [] itemId = {101,102,103,104,105};
        String [] contentArray = {"一","二","三","四","五"};
        MaterialDialog materialDialog = new MaterialDialog.Builder(this)
                .items(contentArray)//添加item内容数组
                .itemsIds(itemId)//添加item的id
                .itemsCallback(new MaterialDialog.ListCallback() { //点击回调
                    @Override
                    public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                        Log.e("test", "onSelection: id="+itemView.getId() );

                    }
                })
                .build();
        materialDialog.show();
    }

效果图:

RecyclerView实现单选列表例子

MaterialDialog materialDialog = new MaterialDialog.Builder(this)
                .title("标题")
                .adapter(mRecyclerViewAdapter,new LinearLayoutManager(this))
                .positiveText(R.string.cancel)
                .build();
        materialDialog.show();

注意这里的.adapter(mRecyclerViewAdapter,new LinearLayoutManager(this)), 适配器一定是RecyclerView的,不能使用ListView,而new LinearLayoutManager(this) 其实就是RecyclerView 布局方向参数.

item点击监听,请参考RecyclerView的具体用法,直接在适配器里实现点击监听.

 

posted on 2019-01-24 09:51  观心静 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/guanxinjing/p/10312793.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值