前言

  开始之前还是需要废话一下,因为有一些坑需要告知。首先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的具体用法,直接在适配器里实现点击监听.

输入框

private static MaterialDialog tipsDialog(Context context) {
MaterialDialog tipsDialog = new MaterialDialog.Builder(context)
.title("进入开发模式")
.input("请输入密码", "", new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) { }
})
.cancelable(false)
.positiveText("确定")
.negativeText("取消")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
L.e("输入的密码内容=" + dialog.getInputEditText().getText().toString());
if (TextUtils.isEmpty(dialog.getInputEditText().getText().toString())) {
Toast.makeText(context, "您没有输入密码", Toast.LENGTH_SHORT).show();
dialog.dismiss();
dialog = null;
return;
}
if (!dialog.getInputEditText().getText().toString().equals(PASSWORD)) {
Toast.makeText(context, "密码错误", Toast.LENGTH_SHORT).show();
dialog.dismiss();
dialog = null;
return;
}
Toast.makeText(context, "密码正确", Toast.LENGTH_SHORT).show();
dialog.dismiss();
dialog = null; }
})
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
dialog.dismiss();
dialog = null; }
})
.build();
return tipsDialog; }

Android 开发 MaterialDialog框架的详解的更多相关文章

  1. Android热门网络框架Volley详解[申明:来源于网络]

    Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html

  2. Android开发——事件分发机制详解

    0. 前言   转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52566965 深入学习事件分发机制,是为了解决在Android开发中 ...

  3. Android开发 ExpandableListView 可折叠列表详解

    前言 在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListView. 其实这个View的原始设计还是ListView的那套.就是增加2层的ListView而已 ...

  4. Android开发:程序目录结构详解

    HelloWorld程序的目录结构概述 我们可以在文件夹中看到,HelloWorld程序的目录主要包括:src文件夹.gen文件夹.Android文件夹.assets.res文件夹. AndroidM ...

  5. Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...

  6. Android组件化框架项目详解

    简介 什么是组件化? 项目发展到一定阶段时,随着需求的增加以及频繁地变更,项目会越来越大,代码变得越来越臃肿,耦合会越来越多,开发效率也会降低,这个时候我们就需要对旧项目进行重构即模块的拆分,官方的说 ...

  7. Android热门网络框架Volley详解

    .Volley简介 volley的英文意思为‘群发’.‘迸发’.Volley是2013年谷歌官方发布的一款Android平台上的网络通信库.Volley非常适合一些数据量不大,但需要频繁通信的网络操作 ...

  8. JavaWeb开发SSM框架搭建详解

    1.需要用到的jar包:由于很多的jar包不好下载,我直接上传到百度网盘: 很多,而且不好下载,我已经整理好好了: 链接:https://pan.baidu.com/s/1iIFprmstp86uKz ...

  9. Android 开发 存储目录的详解

    简介 Android设备,有3个地方的文件存储位置,他们分别是: 内部存储空间(用户无法浏览到此目录) 外部存储空间(就是手机自身的文件管理目录,用户可以浏览) SD卡的存储空间(需要插入T卡) Sh ...

随机推荐

  1. js 实现仿 淘宝 五星评价 demo

    <style> @font-face { font-family: 'iconfont'; /* project id 247957 */ src: url('//at.alicdn.co ...

  2. Ubuntu使用命令行打印文件

    Ubuntu使用命令行打印文件 正文 环境: Ubuntu 16.04.3 LTS HP Deskjet InkAdvantage 4648 准备步骤 安装Common UNIX Printing S ...

  3. Privoxy代理的使用

    目录 1. Privoxy介绍 1.1 安装 1.2 配置 1.3 使用 1.4 其他配置 1.5 测试链接及查看配置 2. 相关信息 https://www.privoxy.org/ http:// ...

  4. vue--模板语法

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name=& ...

  5. VUE路由携带参数的三种方式

    vue 通过路由在进行页面跳转时,会经常携带参数用于同步页面间的数据 路由中携带参数的方式总结如下: 路由定义示例: { name: 'list', path: '/list', component: ...

  6. C++中的内部类

    1.内部类的概念 如果一个类定义在另一个类的内部,这个内部类就叫做内部类.注意此时这个内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去调用内部类.外部类对内部类没有任何优越的访问权限. ...

  7. Computer Graphics Principles And Practice (James Foley / Andries Van Dam / Morgan McGuire / David Sklar / James D. Foley 著)

    1 Introduction 2 Introduction to 2D Graphics Using WPF 3 An Ancient Renderer Made Modern 4 A 2D Grap ...

  8. 剑指offer 5.栈和队列 用两个栈实现队列

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型.   解题思路:1,整体思路是元素先依次进入栈1,再从栈1依次弹出到栈2,然后弹出栈2顶部的元素,整个过程 ...

  9. Hbase各版本环境要求

    1.HBase各版本JDK支持情况           HBase Version JDK 7 JDK 8 JDK 9 JDK 10 2.0 Not Supported yes Not Support ...

  10. hanlp 加载远程词库示例

    说明 ·目前的实现方式是以远程词库的内容重新构建CustomDictionary.trie,demo主要是为了实现同步远程词库,对性能暂不作考虑,对性能要求要以CustomDictionary.dat ...