对话框material-dialogs使用
在build.gradle中添加依赖
compile 'com.afollestad.material-dialogs:core:0.9.0.2'
最简单的对话框,包括标题,内容及是否同意
new MaterialDialog.Builder(getContext())
.title("title")
.content("content")
.positiveText("同意")
.negativeText("不同意").show();
简单对话框
设置点击事件
new MaterialDialog.Builder(getContext())
.title("title")
.content("content")
.positiveText("同意")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getContext(), "同意", Toast.LENGTH_SHORT).show();
}
})
.negativeText("不同意")
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
Toast.makeText(getContext(), "不同意", Toast.LENGTH_SHORT).show();
}
}).show();
设置对话框图标
new MaterialDialog.Builder(getContext())
.title("title")
.content("content")
.positiveText("同意")
.icon(getResources().getDrawable(R.drawable.my_selected)).maxIconSize(200)
.negativeText("不同意").show();
设置图标及大小
设置确认框
.checkBoxPrompt("下次不再提醒", true, new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(!b)
Toast.makeText(getContext(), "确认下次不再提醒", Toast.LENGTH_SHORT).show();
}
})
设置单选框
new MaterialDialog.Builder(getContext())
.title("title")
.content("content")
.positiveText("同意")
.icon(getResources().getDrawable(R.drawable.my_selected)).maxIconSize(200)
.items(R.array.items)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
Toast.makeText(getContext(), "选中的是第"+which+"个,文字为"+text, Toast.LENGTH_SHORT).show();
return true;
}
})
.negativeText("不同意").show();
单选框
多选框
.itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
return false;
}
})
设置输入框
输入文字
.input("请输入", "正在输入", false, new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
}
})
设置进度条
.progress(true,20)
网友评论