因为使用了Lambda表达式,所以要在build.gradle中指定JDK版本
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
直接加最新的就行
implementation 'com.afollestad.material-dialogs:core:3.1.1'
implementation 'com.afollestad.material-dialogs:input:3.1.1'
implementation 'com.afollestad.material-dialogs:files:3.1.1'
implementation 'com.afollestad.material-dialogs:color:3.1.1'
implementation 'com.afollestad.material-dialogs:datetime:3.1.1'
implementation 'com.afollestad.material-dialogs:bottomsheets:3.1.1'
implementation 'com.afollestad.material-dialogs:lifecycle:3.1.1'
private void basic_checkbox_titled_buttons() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.useGoogleLocationServices, null);
dialog.message(R.string.useGoogleLocationServicesPrompt, null, null);
dialog.positiveButton(R.string.agree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.agree), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(R.string.disagree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.disagree), Toast.LENGTH_SHORT).show();
return null;
});
DialogCheckboxExtKt.checkBoxPrompt(dialog, R.string.checkboxConfirm, null, false, isChecked -> {
Toast.makeText(this, "checked? " + isChecked, Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void list_titled_message_buttons() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.socialNetworks, null);
dialog.message(R.string.useGoogleLocationServices, null, null);
DialogListExtKt.listItems(dialog, R.array.socialNetworks, null, null, true, (materialDialog, index, text) -> {
Toast.makeText(this, "Selected item " + text + " at index " + index, Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.agree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.agree), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(R.string.disagree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.disagree), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void single_choice_disabled_items() {
int[] disabledIndices = {1, 3};
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.socialNetworks, null);
DialogSingleChoiceExtKt.listItemsSingleChoice(dialog, R.array.socialNetworks, null, disabledIndices, 0,
true, (materialDialog, index, text) -> {
Toast.makeText(this, "Selected item " + text + " at index " + index, Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.choose, null, materialDialog -> {
Toast.makeText(this, getText(R.string.choose), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void multiple_choice_disabled_items() {
int[] initialSelection = {2, 3};
int[] disabledIndices = {1, 3};
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.socialNetworks, null);
DialogMultiChoiceExtKt.listItemsMultiChoice(dialog, R.array.socialNetworks, null, disabledIndices, initialSelection,
true, false, (materialDialog, indices, text) -> {
Toast.makeText(this, "Selected item " + text + " at indices " + indices.length, Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.choose, null, materialDialog -> {
Toast.makeText(this, getText(R.string.choose), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void buttons_stacked_checkboxPrompt() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.useGoogleLocationServices, null);
dialog.message(R.string.useGoogleLocationServicesPrompt, null, null);
dialog.positiveButton(null, "Hello World", materialDialog -> {
Toast.makeText(this, "Hello World", Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(null, "How are you doing?", materialDialog -> {
Toast.makeText(this, "How are you doing?", Toast.LENGTH_SHORT).show();
return null;
});
dialog.neutralButton(null, "Testing long buttons", materialDialog -> {
Toast.makeText(this, "Testing long buttons", Toast.LENGTH_SHORT).show();
return null;
});
DialogCheckboxExtKt.checkBoxPrompt(dialog, R.string.checkboxConfirm, null, false, isChecked -> {
Toast.makeText(this, "checked? " + isChecked, Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void input_check_prompt() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.useGoogleLocationServices, null);
dialog.message(R.string.useGoogleLocationServicesPrompt, null, null);
DialogInputExtKt.input(dialog, "Type something", null, null, null,
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS ,
null, true, false, (materialDialog, text) -> {
Toast.makeText(this, "Input " + text, Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.agree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.agree), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(R.string.disagree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.disagree), Toast.LENGTH_SHORT).show();
return null;
});
DialogCheckboxExtKt.checkBoxPrompt(dialog, R.string.checkboxConfirm, null, false, isChecked -> {
Toast.makeText(this, "checked? " + isChecked, Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void custom_view_webview() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
DialogCustomViewExtKt.customView(dialog, R.layout.custom_view_webview, null,
false, true, false, false);
dialog.setOnShowListener(onShowListener -> {
WebView webView = DialogCustomViewExtKt.getCustomView(dialog).findViewById(R.id.web_view);
webView.loadData(
"WebView Custom View
\n" +
"\n" +
"\n"
+
" NEW: Hey! \n" +
" IMPROVE: Hello! \n" +
" FIX: Hi! \n" +
" FIX: Hey again! \n" +
" FIX: What? \n" +
" FIX: This is an example. \n" +
" MISC: How are you? \n" +
"\n" +
"Material guidelines for dialogs:\n"
+
" " +
"http://www.google.com/design/spec/components/dialogs.html.\n" +
"",
"text/html",
"UTF-8");
});
dialog.show();
}
private void colorChooser_primary_customRgb() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.primary_colors, null);
DialogColorChooserExtKt.colorChooser(dialog, ColorPalette.INSTANCE.getPrimary(), ColorPalette.INSTANCE.getPrimarySub(),
null, true, true, false, false,
(materialDialog, color) -> {
Toast.makeText(this, "Selected color: " + Integer.toHexString(color), Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.select, null, materialDialog -> {
Toast.makeText(this, getText(R.string.select), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(android.R.string.cancel, null, materialDialog -> {
Toast.makeText(this, getText(android.R.string.cancel), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void file_chooser_filter() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(R.string.primary_colors, null);
DialogFileChooserExtKt.fileChooser(dialog, getExternalStorageDirectory(), file -> {
return (file.getName().endsWith("txt"));
},
true, R.string.files_default_empty_text, true, null,
(materialDialog, file) -> {
Toast.makeText(this, "Selected file: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.select, null, materialDialog -> {
Toast.makeText(this, getText(R.string.select), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(android.R.string.cancel, null, materialDialog -> {
Toast.makeText(this, getText(android.R.string.cancel), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void datetime_picker() {
MaterialDialog dialog = new MaterialDialog(this, MaterialDialog.getDEFAULT_BEHAVIOR());
dialog.title(null, "Select Date and Time");
DateTimePickerExtKt.dateTimePicker(dialog, null, null, false,
false, true,
(materialDialog, dateTime) -> {
Toast.makeText(this, "Selected date/time: " + dateTime.getTime(), Toast.LENGTH_SHORT).show();
return null;
});
dialog.positiveButton(R.string.select, null, materialDialog -> {
Toast.makeText(this, getText(R.string.select), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(android.R.string.cancel, null, materialDialog -> {
Toast.makeText(this, getText(android.R.string.cancel), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
private void bottomsheet_info() {
BottomSheet bottomSheet = new BottomSheet(LayoutMode.WRAP_CONTENT);
MaterialDialog dialog = new MaterialDialog(this, bottomSheet);
dialog.title(R.string.useGoogleLocationServices, null);
dialog.message(R.string.useGoogleLocationServicesPrompt, null, null);
dialog.positiveButton(R.string.agree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.agree), Toast.LENGTH_SHORT).show();
return null;
});
dialog.negativeButton(R.string.disagree, null, materialDialog -> {
Toast.makeText(this, getText(R.string.disagree), Toast.LENGTH_SHORT).show();
return null;
});
dialog.show();
}
https://github.com/liuliangchao/MaterialDialogsSampleJava