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的具体用法,直接在适配器里实现点击监听.
输入框
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框架的详解的更多相关文章
- Android热门网络框架Volley详解[申明:来源于网络]
Android热门网络框架Volley详解[申明:来源于网络] 地址:http://www.cnblogs.com/caobotao/p/5071658.html
- Android开发——事件分发机制详解
0. 前言 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/52566965 深入学习事件分发机制,是为了解决在Android开发中 ...
- Android开发 ExpandableListView 可折叠列表详解
前言 在需要实现一个List的item需要包含列表的时候,我们就可以选择ExpandableListView. 其实这个View的原始设计还是ListView的那套.就是增加2层的ListView而已 ...
- Android开发:程序目录结构详解
HelloWorld程序的目录结构概述 我们可以在文件夹中看到,HelloWorld程序的目录主要包括:src文件夹.gen文件夹.Android文件夹.assets.res文件夹. AndroidM ...
- Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)
在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...
- Android组件化框架项目详解
简介 什么是组件化? 项目发展到一定阶段时,随着需求的增加以及频繁地变更,项目会越来越大,代码变得越来越臃肿,耦合会越来越多,开发效率也会降低,这个时候我们就需要对旧项目进行重构即模块的拆分,官方的说 ...
- Android热门网络框架Volley详解
.Volley简介 volley的英文意思为‘群发’.‘迸发’.Volley是2013年谷歌官方发布的一款Android平台上的网络通信库.Volley非常适合一些数据量不大,但需要频繁通信的网络操作 ...
- JavaWeb开发SSM框架搭建详解
1.需要用到的jar包:由于很多的jar包不好下载,我直接上传到百度网盘: 很多,而且不好下载,我已经整理好好了: 链接:https://pan.baidu.com/s/1iIFprmstp86uKz ...
- Android 开发 存储目录的详解
简介 Android设备,有3个地方的文件存储位置,他们分别是: 内部存储空间(用户无法浏览到此目录) 外部存储空间(就是手机自身的文件管理目录,用户可以浏览) SD卡的存储空间(需要插入T卡) Sh ...
随机推荐
- 20164318 毛瀚逸 Exp1 PC平台逆向破解
一.逆向及Bof基础实践说明 1.1实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 手工修 ...
- [Ynoi2019模拟赛]Yuno loves sqrt technology III
题目大意: 给你一个长为n的序列a,m次询问,每次查询一个区间的众数的出现次数,强制在线. 解题思路: 出题人题解 众所周知lxl是个毒瘤,Ynoi道道都是神仙题 首先得离散化. 分块后,预处理Fi, ...
- 如何处理Excel空行问题
在操作excel的时候, 可能会出现很多的无效数据行. 下面是一个我的简单处理方式 public static bool DataSetToExcel(DataSet dataSet, string ...
- InsertSort
#include <bits/stdc++.h> using namespace std; #define MAXSIZE 200000 typedef int KeyType; type ...
- C#中的参数和调用方式(可选参数、具名参数、可空参数)
具名参数 和 可选参数 是 C# framework 4.0 出来的新特性. 一. 常规方法定义及调用 public void Demo1(string x, int y) { //do someth ...
- linux一些工具的安装(三)
linux(vmware15 centos7)中Docker安装 一.Docker卸载 1.查看已安装的docker安装包 $yum list installed|grep docker 执行后的 ...
- 嵌入式C语言预处理使用
#include 包含头文件 #define 宏 #define 宏名 (宏体) //不进行语法检查 #define ABC(x) (5+(x)) //宏函数 #define #else ...
- delphi 多线程之System.TMonitor (续一)
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- 新手尝试Android studio连接mumu调试程序
由于Android studio本身虚拟机比较卡在安装as的时候就没有安装.于是自己安装了一款手机模拟器mumu模拟器.我想真机可以调试那么摸仪器应该也可以,于是就从网上找资料,其实连接很简单. 1. ...
- Kettle解决方案: 第三章 安装和配置