dialog第三方框架,material-dialogs的使用
2017-12-12 14:17
1166 查看
之前浏览github的时候发现一个比较好用的MD风格的Dialog。这里记录一下使用。
Github地址:https://github.com/afollestad/material-dialogs
2
Basic Dialog
简单的dialog
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
相应的效果:
简单的diaolog,同意和不同意的字体颜色是默认是R.color.colorAccent
文本变多的时候会自动拉长高度
嵌套一个recycleview,这个的点击事件可以先获取此Recycleview对象然后自己处理
2
3
4
这里添加一个更多信息的按钮
点击事件
方式一
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
就是传一个SingleButtonCallback 接口,用DialogAction 来区分现在是那个Action 请求,然后对应的处理
方式二
2
3
4
5
6
7
8
9
10
单个按钮添加监听和onAny 其实一样
方式三
这个方法已经过时了
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
添加checkbox
2
3
4
5
6
7
8
9
10
颜色修改
其他属性介绍
2
3
4
5
6
7
List Dialogs
列表弹框,.item ()来添加类别内容,也可以是
2
3
4
5
6
7
8
列表点击事件
2
3
4
5
6
7
添加单选
2
3
4
5
6
7
8
9
Multi Choice List Dialogs
多选dialog
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
这里有一个itemsCallbackMultiChoice()他的返回结果决定了checkbox 是否有选中和取消的效果。
Custom Views
可以引入外部view的dialog
2
3
效果:
customView( int layoutRes, boolean wrapInScrollView)
当我们将wrapInScrollView设置为true的时候就表示需要一个padding
假设布局中有个按钮我们要点击这个按钮关闭dialog可以这样操作
2
3
4
5
6
7
8
9
10
11
12
13
Input Dialogs
含有输入框的弹框
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
限制条件
输入类型是 .inputType(InputType.TYPE_CLASS_PHONE)//可以输入的类型-电话号码,限制输入长度是inputRange(11, 41, R.color.colorAccent)//限制输入的长度,只有在(11,41)长度内ok按钮才是可用的。限制长度还可以是在onInput()方法里处理。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Progress Dialog
进度条弹框应该是用的比较多的了,但是这个的我感觉用起来不好,一般的点击加载进度
设置成水平的进度条
2
//progress(boolean indeterminate, int max, boolean showMinMax)
// false 的话是水平进度条,true是等待圆环 最大值 是否显示数值,false的时候是不可以用progress这类的属性的灰报错:Cannot use setProgress() on this dialog.
ColorDialog
有关颜色选择的相关弹框,选择的时候实现选择的回掉接口:ColorChooserDialog.ColorCallback
2
3
4
5
6
2
3
4
5
6
7
8
9
可以自己定义筛选的颜色
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
File Selector Dialogs
文件弹框
2
3
4
5
6
7
实现接口
@Override
public void onFileSelection(@NonNull FileChooserDialog dialog, @NonNull File file) {
//选择文件
}
Github地址:https://github.com/afollestad/material-dialogs
导入
compile 'com.afollestad.material-dialogs:core:0.9.1.0' compile 'com.afollestad.material-dialogs:commons:0.9.1.0'1
2
具体的使用
Basic Dialog简单的dialog
new MaterialDialog.Builder(MainActivity.this) .title("basic dialog") .content("一个简单的dialog,高度会随着内容改变,同时还可以嵌套RecyleView") .iconRes(R.drawable.icon) .positiveText("同意") .negativeText("不同意") .neutralText("更多信息") .widgetColor(Color.BLUE)//不再提醒的checkbox 颜色 //CheckBox .checkBoxPrompt("不再提醒", false, new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { Toast.makeText(MainActivity.this, "不再提醒", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "会再次提醒", Toast.LENGTH_LONG).show(); } } }) //嵌套recycleview,这个的点击事件可以先获取此Recycleview对象然后自己处理 .adapter(new RecycleviewAdapter(getData(), MainActivity.this), new LinearLayoutManager(MainActivity.this)) .itemsCallback(new MaterialDialog.ListCallback() { @Override public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { dataChoose = "下标:" + position + " and 数据:" + mData.get(position); } }) //点击事件添加 方式1 .onAny(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { if (which == DialogAction.NEUTRAL) { Toast.makeText(MainActivity.this, "更多信息", Toast.LENGTH_LONG).show(); } else if (which == DialogAction.POSITIVE) { Toast.makeText(MainActivity.this, "同意" + dataChoose, Toast.LENGTH_LONG).show(); } else if (which == DialogAction.NEGATIVE) { Toast.makeText(MainActivity.this, "不同意", Toast.LENGTH_LONG).show(); } } }) .show();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
相应的效果:
简单的diaolog,同意和不同意的字体颜色是默认是R.color.colorAccent
文本变多的时候会自动拉长高度
嵌套一个recycleview,这个的点击事件可以先获取此Recycleview对象然后自己处理
.adapter(new RecycleviewAdapter(getData(), MainActivity.this), new LinearLayoutManager(MainActivity.this)) // RecycleView rc= dialog.getRecyclerView();1
2
3
4
这里添加一个更多信息的按钮
.neutralText("更多信息")1
点击事件
方式一
.onAny(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { if (which == DialogAction.NEUTRAL) { Toast.makeText(MainActivity.this, "更多信息", Toast.LENGTH_LONG).show(); } else if (which == DialogAction.POSITIVE) { Toast.makeText(MainActivity.this, "同意" + dataChoose, Toast.LENGTH_LONG).show(); } else if (which == DialogAction.NEGATIVE) { Toast.makeText(MainActivity.this, "不同意", Toast.LENGTH_LONG).show(); } } })1
2
3
4
5
6
7
8
9
10
11
12
13
public Builder onAny(@NonNull SingleButtonCallback callback) { this.onAnyCallback = callback; return this; } public interface SingleButtonCallback { void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which); } public enum DialogAction { POSITIVE, NEUTRAL, NEGATIVE }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
就是传一个SingleButtonCallback 接口,用DialogAction 来区分现在是那个Action 请求,然后对应的处理
方式二
.onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { Toast.makeText(MainActivity.this,"同意",Toast.LENGTH_LONG).show(); } }) //.onNegative() //.onNeutral()1
2
3
4
5
6
7
8
9
10
单个按钮添加监听和onAny 其实一样
方式三
这个方法已经过时了
.callback(new MaterialDialog.ButtonCallback() {//添加按钮点击监听 @Override public void onPositive(MaterialDialog dialog) { super.onPositive(dialog); Toast.makeText(MainActivity.this,"同意",Toast.LENGTH_LONG).show(); } @Override public void onNegative(MaterialDialog dialog) { super.onNegative(dialog); Toast.makeText(MainActivity.this,"不同意",Toast.LENGTH_LONG).show(); } @Override public void onNeutral(MaterialDialog dialog) { super.onNeutral(dialog); Toast.makeText(MainActivity.this,"更多信息",Toast.LENGTH_LONG).show(); } })1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
添加checkbox
.checkBoxPrompt("不再提醒", false, new CompoundButton.OnCheckedChangeListener() {//check事件 @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { Toast.makeText(MainActivity.this, "不再提醒", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "会再次提醒", Toast.LENGTH_LONG).show(); } } })1
2
3
4
5
6
7
8
9
10
颜色修改
.widgetColor(Color.BLUE)1
其他属性介绍
.btnSelector(R.color.colorPrimary)//按钮的背景颜色 //分开设置2个按钮的背景颜色 // .btnSelector(R.color.colorPrimary, DialogAction.NEGATIVE) // .btnSelector(R.color.colorPrimaryDark, DialogAction.POSITIVE) // .btnSelector(R.color.colorPrimary,DialogAction.NEUTRAL) // .backgroundColor(Color.parseColor("#FF9988"))//dialog的背景颜色 // .contentColor(Color.WHITE)//内容字体的颜色1
2
3
4
5
6
7
List Dialogs
列表弹框,.item ()来添加类别内容,也可以是
.items(new String[]{"AAAA","BBBBB","CCCCC","DDDDDDDD","EEEEE","FFFFFF","GGGGGG","HHHHHHH"})1
new MaterialDialog.Builder(MainActivity.this) .title("List Dialog") .iconRes(R.drawable.ic_logo) .content("List Dialog,显示数组信息,高度会随着内容扩大") .items(R.array.item) //.listSelector(R.color.green)//列表的背景颜色 .autoDismiss(false)//不自动消失 .show();1
2
3
4
5
6
7
8
列表点击事件
.itemsCallback(new MaterialDialog.ListCallback() {//选中监听,同时dialog消失 @Override public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) { dataChoose += "下标:" + position + " and 数据:" + text; Toast.makeText(MainActivity.this, dataChoose, Toast.LENGTH_LONG).show(); } })1
2
3
4
5
6
7
添加单选
//单选 .itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {//0 表示第一个选中 -1 不选 @Override public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) { dataChoose="此时选中的下标"+which; Toast.makeText(MainActivity.this,dataChoose,Toast.LENGTH_LONG).show(); return true; } })1
2
3
4
5
6
7
8
9
Multi Choice List Dialogs
多选dialog
new MaterialDialog.Builder(MainActivity.this) .title("Multi Choice List Dialogs") .iconRes(R.drawable.ic_logo) .content("Multi Choice List Dialogs,显示数组信息,高度会随着内容扩大.可以多选") .items(R.array.item) .positiveText("确定") .widgetColor(Color.RED)//改变checkbox的颜色 //多选框添加 .itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() { @Override public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) { return true;//false 的时候没有选中样式 } }) //点击确定后获取选中的下标数组 .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { dialog.dismiss(); Toast.makeText(MainActivity.this, "选中" + dialog.getSelectedIndices().length + "个", Toast.LENGTH_LONG).show(); } }) .show();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
这里有一个itemsCallbackMultiChoice()他的返回结果决定了checkbox 是否有选中和取消的效果。
Custom Views
可以引入外部view的dialog
new MaterialDialog.Builder(MainActivity.this) .customView(R.layout.custome_view,false) .show();1
2
3
效果:
customView( int layoutRes, boolean wrapInScrollView)
当我们将wrapInScrollView设置为true的时候就表示需要一个padding
假设布局中有个按钮我们要点击这个按钮关闭dialog可以这样操作
final MaterialDialog dialog = new MaterialDialog.Builder(MainActivity.this) .customView(R.layout.custome_view, false) .show(); View customeView = dialog.getCustomView(); Button button = (Button) customeView.findViewById(R.id.btn_closeCustome); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } });1
2
3
4
5
6
7
8
9
10
11
12
13
Input Dialogs
含有输入框的弹框
new MaterialDialog.Builder(MainActivity.this)1
.title("输入窗")
.iconRes(R.drawable.ic_logo)
.content("包含输入框的diaolog")
// .widgetColor(Color.BLUE)//输入框光标的颜色
.inputType(InputType.TYPE_CLASS_PHONE)//可以输入的类型-电话号码
//前2个一个是hint一个是预输入的文字
.input(R.string.input_hint, R.string.input_prefill, new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
Log.i("yqy", "输入的是:" + input);
}
})
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
if (dialog.getInputEditText().length() <=10) {
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
}else {
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
}
}
})
.show();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
限制条件
输入类型是 .inputType(InputType.TYPE_CLASS_PHONE)//可以输入的类型-电话号码,限制输入长度是inputRange(11, 41, R.color.colorAccent)//限制输入的长度,只有在(11,41)长度内ok按钮才是可用的。限制长度还可以是在onInput()方法里处理。
if (dialog.getInputEditText().length() < 10) { dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false); }else{ dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true); } while (dialogPro.getCurrentProgress()!=dialogPro.getMaxProgress()){ if (dialogPro.isCancelled()) break; try { Thread.sleep(50);//模拟加载时间 } catch (InterruptedException e) { break; } dialogPro.incrementProgress(1); } dialogPro.setContent("加载完成");1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Progress Dialog
进度条弹框应该是用的比较多的了,但是这个的我感觉用起来不好,一般的点击加载进度
设置成水平的进度条
.progress(false, 100, true) .progressNumberFormat("%1d/%2d")1
2
//progress(boolean indeterminate, int max, boolean showMinMax)
// false 的话是水平进度条,true是等待圆环 最大值 是否显示数值,false的时候是不可以用progress这类的属性的灰报错:Cannot use setProgress() on this dialog.
没有一个加载进度的过程1
ColorDialog
有关颜色选择的相关弹框,选择的时候实现选择的回掉接口:ColorChooserDialog.ColorCallback
@Override public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) { //此时选择的颜色 }1
2
3
4
5
6
new ColorChooserDialog.Builder(MainActivity.this,R.string.app_name) .titleSub(R.string.input_hint) // title of dialog when viewing shades of a color .accentMode(false) // when true, will display accent palette instead of primary palette .doneButton(R.string.md_done_label) // changes label of the done button .cancelButton(R.string.md_cancel_label) // changes label of the cancel button .backButton(R.string.md_back_label) // changes label of the back button .preselect(Color.RED) // 开始的时候的默认颜色 .dynamicButtonColor(true) // defaults to true, false will disable changing action buttons' color to currently selected color .show();1
2
3
4
5
6
7
8
9
可以自己定义筛选的颜色
int[] primary = new int[] { Color.parseColor("#F44336") }; int[][] secondary = new int[][] { new int[] { Color.parseColor("#EF5350"), Color.parseColor("#F44336"), Color.parseColor("#E53935") } }; new ColorChooserDialog.Builder(MainActivity.this, R.string.app_name) // .titleSub(R.string.app_name) .customColors(primary, secondary) .doneButton(R.string.done) .cancelButton(R.string.cancel) .titleSub(R.string.done)//设置二级选择的标题 // .presetsButton(R.string.input_hint)//从RRGB切换到CUstome的文字提示 .show();1
2
3
4
5
6
7
8
9
10
11
12
13
14
new ColorChooserDialog.Builder(MainActivity.this, R.string.app_name) .allowUserColorInput(false) .customButton(R.string.md_custom_label) .presetsButton(R.string.md_presets_label) .show();1
2
3
4
5
File Selector Dialogs
文件弹框
new FileChooserDialog.Builder(MainActivity.this) .initialPath("/sdcard/Download") // changes initial path, defaults to external storage directory .mimeType("image/*") // Optional MIME type filter .extensionsFilter(".png", ".jpg") // Optional extension filter, will override mimeType() .tag("optional-identifier") .goUpLabel("Up") // custom go up label, default label is "..." .show();1
2
3
4
5
6
7
实现接口
@Override
public void onFileSelection(@NonNull FileChooserDialog dialog, @NonNull File file) {
//选择文件
}
相关文章推荐
- iso中第三方框架SDWebImage的使用步骤
- ios数据存储中关于Sqlite3使用第三方框架FMDB
- 获取通信录(使用第三方框架RHAddressBook)
- Android第三方框架 Universal-Image-Loader的使用,进一步封装
- Android开发使用的常见第三方框架汇总
- Android第三方框架的设置、导入、使用
- 第三方框架Cocoapod使用
- Android开发使用的常见第三方框架汇总
- 【FastDev4Android框架开发】AndroidAnnnotations注入框架使用之第三方框架集成OrmLite(十五)
- 第三方okhttp网络访问框架使用教程
- 使用第三方框架实现异步的HttpClient
- 关于第三方IOS的checkBox框架的使用
- 使用Cocoapod导入第三方框架
- Android开发使用的常见第三方框架汇总
- ios socket第三方框架 AsyncSocket使用简介,连接,心跳,断线,数据发送与接收
- Android第三方框架之xListView的使用方法
- SQLite3第三方框架——FMDB的基本使用
- Android开发使用的常见第三方框架汇总
- 关于第三方IOS的checkBox框架的使用
- xUtils数据库第三方框架使用步骤