新版material-dialogs的java详细使用方法

新版material-dialogs的java详细使用方法

    • 注意
    • 添加依赖
    • 信息弹出框
    • 列表弹出框
    • 单选单出框,带屏蔽条目
    • 复选单出框,带屏蔽条目
    • 堆按钮,带单选框
    • 输入弹出框,带单选
    • WebView弹出框
    • 颜色选择器,带自定义颜色
    • 文件选择器,带过滤器
    • 时间选择器
    • 底部弹出框
    • 完整java版例程

新版的 material-dialogs已经改用Kotlin,而且sample也是使用Kotlin写的。想到介绍下如何使用java来调用,毕竟不能老是用老版本的吧。将原来Kotlin写的例子,用java完整改写了一遍。详细的所有用法,集成在文后的项目源码里。这里着重介绍几个代表的用法。

注意

因为使用了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();
}

新版material-dialogs的java详细使用方法_第1张图片

列表弹出框

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();
}

新版material-dialogs的java详细使用方法_第2张图片

单选单出框,带屏蔽条目

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();
}

新版material-dialogs的java详细使用方法_第3张图片

复选单出框,带屏蔽条目

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();
}

新版material-dialogs的java详细使用方法_第4张图片

堆按钮,带单选框

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();
}

新版material-dialogs的java详细使用方法_第5张图片

输入弹出框,带单选

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();
}

新版material-dialogs的java详细使用方法_第6张图片

WebView弹出框

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" + "
  1. NEW: Hey!
  2. \n"
    + "
  3. IMPROVE: Hello!
  4. \n"
    + "
  5. FIX: Hi!
  6. \n"
    + "
  7. FIX: Hey again!
  8. \n"
    + "
  9. FIX: What?
  10. \n"
    + "
  11. FIX: This is an example.
  12. \n"
    + "
  13. MISC: How are you?
  14. \n"
    + "
\n"
+ "

Material guidelines for dialogs:\n" + " " + "http://www.google.com/design/spec/components/dialogs.html.\n" + "

"
, "text/html", "UTF-8"); }); dialog.show(); }

新版material-dialogs的java详细使用方法_第7张图片

颜色选择器,带自定义颜色

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();
}

新版material-dialogs的java详细使用方法_第8张图片

文件选择器,带过滤器

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();
}

新版material-dialogs的java详细使用方法_第9张图片

时间选择器

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();
}

新版material-dialogs的java详细使用方法_第10张图片

底部弹出框

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();
}

新版material-dialogs的java详细使用方法_第11张图片

完整java版例程

https://github.com/liuliangchao/MaterialDialogsSampleJava

你可能感兴趣的:(Android)