文章目录
core版:可以创建basic, list, single/multi choice, progress, input等弹框。
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
common版:可以创建更多拓展的弹框包括ColorChooserDialog, FolderChooserDialog, the Material Preference classes, and MaterialSimpleListAdapter/MaterialSimpleListItem等等。
implementation 'com.afollestad.material-dialogs:commons:0.9.6.0'
1、基础弹框
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认")
.negativeText("取消")
.show();
2、取消弹框
得这样new
MaterialDialog dialog = new MaterialDialog.Builder(this)
.title(R.string.title)
.content(R.string.content)
.positiveText(R.string.agree)
.show();
这样取消
dialog.dismiss();
3、添加图标
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认")
.negativeText("取消")
.icon(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp))
.show();
可以用maxIconSize(int size)
限制最大size
4、自适应按钮
如果按钮太长,它会自动纵向排列
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认确认确认确认确认确认确认确认确认确认")
.negativeText("取消取消取消取消取消取消取消取消取消取消")
.icon(getResources().getDrawable(R.drawable.ic_account_circle_black_24dp))
.show();
5、中立按钮
除了确认类和取消类按钮还有中立按钮,它会在最左边
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认")
.negativeText("取消")
.neutralText("更多")
.show();
6、回调
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认")
.negativeText("取消")
.neutralText("更多")
.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
// TODO
}
})
.onNeutral(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
// TODO
}
})
.onNegative(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
// TODO
}
})
.show();
如果你想监听三种动作按钮,只用一个onAny就行了,which会告诉你点击了什么按钮,根据需要去判断
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认")
.negativeText("取消")
.neutralText("更多")
.onAny(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
Toast.makeText(MainActivity.this, which.toString(), Toast.LENGTH_SHORT).show();
}
})
.show();
7、CheckBox
new MaterialDialog.Builder(this)
.title("标题")
.content("内容")
.positiveText("确认")
.checkBoxPromptRes(R.string.dont_ask_again, false, null)
.onAny(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(MaterialDialog dialog, DialogAction which) {
Toast.makeText(MainActivity.this, dialog.isPromptCheckBoxChecked()+"", Toast.LENGTH_SHORT).show();
}
})
.show();
8、列表
普通列表
new MaterialDialog.Builder(this)
.title("标题")
.positiveText("确认")
.negativeText("取消")
.items(R.array.items)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
Toast.makeText(MainActivity.this, which+"", Toast.LENGTH_SHORT).show();
}
})
.show();
单选列表
new MaterialDialog.Builder(this)
.title("标题")
.positiveText("确认")
.items(R.array.items)
.itemsCallbackSingleChoice(-1, new MaterialDialog.ListCallbackSingleChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
...
return true;
}
})
.show();
多选列表
new MaterialDialog.Builder(this)
.title("标题")
.positiveText("确认")
.items(R.array.items)
.itemsCallbackMultiChoice(null, new MaterialDialog.ListCallbackMultiChoice() {
@Override
public boolean onSelection(MaterialDialog dialog, Integer[] which, CharSequence[] text) {
return true;
}
})
.show();
指定id的列表
<resources>
<string-array name="items">
<item>aa</item>
<item>bb</item>
<item>cc</item>
</string-array>
<integer-array name="itemIds">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
</resources>
new MaterialDialog.Builder(this)
.title("标题")
.positiveText("确认")
.items(R.array.items)
.itemsIds(R.array.itemIds)
.itemsCallback(new MaterialDialog.ListCallback() {
@Override
public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
Toast.makeText(MainActivity.this, which + ": " + text + ", ID = " + view.getId(), Toast.LENGTH_SHORT).show();
}
})
.show();
9、自定义View
new MaterialDialog.Builder(this)
.title("Title")
.customView(R.layout.item_cart, true)
.positiveText("CHOOSE")
.show();
10、输入框
new MaterialDialog.Builder(this)
.title("Title")
.content("内容")
.inputType(InputType.TYPE_CLASS_TEXT)
.input("说点什么", null, new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
ToastUtils.showToast(MainActivity.this, input);
}
})
.positiveText("确定")
.show();
11、限制有效字数
new MaterialDialog.Builder(this)
.title("Title")
.inputRangeRes(2, 20, R.color.warning)
.inputType(InputType.TYPE_CLASS_TEXT)
.input("说点什么", null, new MaterialDialog.InputCallback() {
@Override
public void onInput(MaterialDialog dialog, CharSequence input) {
ToastUtils.showToast(MainActivity.this, input);
}
})
.positiveText("确定")
.show();
12、加载框
new MaterialDialog.Builder(this)
.title("Progress")
.content("Please Wait...")
.progress(true, 0)
.show();
水平
13、进度条
public class MainActivity extends AppCompatActivity {
private Thread thread;
private void startThread(Runnable run) {
if (thread != null) {
thread.interrupt();
}
thread = new Thread(run);
thread.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void click(View view) {
new MaterialDialog.Builder(this).title("Progress").content("please wait...").contentGravity(GravityEnum.CENTER).progress(false, 150, true).cancelListener(dialog -> {
if (thread != null) {
thread.interrupt();
}
}).showListener(dialogInterface -> {
final MaterialDialog dialog = (MaterialDialog) dialogInterface;
startThread(() -> {
while (dialog.getCurrentProgress() != dialog.getMaxProgress() && !Thread.currentThread().isInterrupted()) {
if (dialog.isCancelled()) {
break;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
dialog.incrementProgress(1);
}
runOnUiThread(() -> {
thread = null;
dialog.setContent(getString(R.string.md_done_label));
});
});
}).show();
}
}
14、样式
主题
new MaterialDialog.Builder(this)
.title("Progress")
.content("Please Wait...")
.theme(Theme.DARK)
.progress(true, 0)
.progressIndeterminateStyle(true)
.show();
颜色
可以设置颜色的有以下这些
.titleColorRes(R.color.material_red_500)
.contentColor(Color.WHITE) // notice no 'res' postfix for literal color
.linkColorAttr(R.attr.my_link_color_attr) // notice attr is used instead of none or res for attribute resolving
.dividerColorRes(R.color.material_pink_500)
.backgroundColorRes(R.color.material_blue_grey_800)
.positiveColorRes(R.color.material_red_500)
.neutralColorRes(R.color.material_red_500)
.negativeColorRes(R.color.material_red_500)
.widgetColorRes(R.color.material_red_500)
.buttonRippleColorRes(R.color.material_red_500)
透明背景
new MaterialDialog.Builder(this)
.title("Progress")
.content("Please Wait...")
.titleColorRes(R.color.white)
.contentColor(Color.WHITE)
.backgroundColorRes(R.color.alpha_black_80)//#80000000
.progress(true, 0)
.show();
Selectors
用来改变drawable被点击或聚焦时的状态
.btnSelector(R.drawable.custom_btn_selector)
.btnSelector(R.drawable.custom_btn_selector_primary, DialogAction.POSITIVE)
.btnSelectorStacked(R.drawable.custom_btn_selector_stacked)
.listSelector(R.drawable.custom_list_and_stackedbtn_selector)
Gravity
改变位置
.titleGravity(GravityEnum.CENTER)
.contentGravity(GravityEnum.CENTER)
.btnStackedGravity(GravityEnum.START)
.itemsGravity(GravityEnum.END)
.buttonsGravity(GravityEnum.END)