您的位置:首页 > 其它

开源项目material-dialogs使用

2017-06-27 15:52 246 查看
之前浏览github的时候发现一个比较好用的MD风格的Dialog。这里记录一下使用。 

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
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
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
1
2
3
4



这里添加一个更多信息的按钮
.neutralText("更多信息")
1
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
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
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
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
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
1
2
3
4
5
6
7
8
9
10



颜色修改
.widgetColor(Color.BLUE)
1
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
1
2
3
4
5
6
7
List Dialogs

列表弹框,.item ()来添加类别内容,也可以是

.items(new String[]{"AAAA","BBBBB","CCCCC","DDDDDDDD","EEEEE","FFFFFF","GGGGGG","HHHHHHH"})
1
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
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
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
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
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
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
1
2
3
4
5
6
7
8
9
10
11
12
13
Input Dialogs

含有输入框的弹框

             new MaterialDialog.Builder(MainActivity.this)
.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();
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
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





限制条件

输入类型是 .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
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
1
2

//progress(boolean indeterminate, int max, boolean showMinMax) 

// false 的话是水平进度条,true是等待圆环 最大值 是否显示数值,false的时候是不可以用progress这类的属性的灰报错:Cannot use setProgress() on this dialog.

没有一个加载进度的过程
1
1



ColorDialog

有关颜色选择的相关弹框,选择的时候实现选择的回掉接口:ColorChooserDialog.ColorCallback

@Override
public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) {
//此时选择的颜色

}
1
2
3
4
5
6
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
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
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
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
1
2
3
4
5
6
7



实现接口 

@Override 

public void onFileSelection(@NonNull FileChooserDialog dialog, @NonNull File file) { 

//选择文件 

}

demo下载 

以上。感谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: