Skip to content
xuexiangjys edited this page Apr 26, 2022 · 8 revisions

Android对话框组件。包括提示性对话框、确认对话框、输入对话框、菜单型对话框、单选、多选对话框、带进度条的对话框、loading对话框、以及自定义对话框等。

Material Dialog

遵循谷歌Material Design设计风格的对话框,api与系统同步,对不同尺寸的设备以及系统版本做了兼容。

  • 简单的提示性对话框

new MaterialDialog.Builder(getContext())
        .iconRes(R.drawable.icon_tip)
        .title(R.string.tip_infos)
        .content(R.string.content_simple_confirm_dialog)
        .positiveText(R.string.lab_submit)
        .show();
        
//使用DialogLoader可自由切换dialog显示的策略
DialogLoader.getInstance().showTipDialog(...);        
  • 简单的确认对话框

new MaterialDialog.Builder(getContext())
        .content(R.string.tip_bluetooth_permission)
        .positiveText(R.string.lab_yes)
        .negativeText(R.string.lab_no)
        .show();
        
//使用DialogLoader可自由切换dialog显示的策略
DialogLoader.getInstance().showConfirmDialog(...);  
  • 简单带输入的对话框

new MaterialDialog.Builder(getContext())
        .iconRes(R.drawable.icon_warning)
        .title(R.string.tip_warning)
        .content(R.string.content_warning)
        .inputType(
                InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                        | InputType.TYPE_TEXT_FLAG_CAP_WORDS)
        .input(
                getString(R.string.hint_please_input_password),
                "",
                false,
                ((dialog, input) -> Toast(input.toString())))
        .inputRange(3, 5)
        .positiveText(R.string.lab_continue)
        .negativeText(R.string.lab_change)
        .onPositive(((dialog, which) -> Toast("你输入了:" + dialog.getInputEditText().getText().toString())))
        .cancelable(false)
        .show();
        
//使用DialogLoader可自由切换dialog显示的策略
DialogLoader.getInstance().showInputDialog(...);          
  • 菜单型对话框

new MaterialDialog.Builder(getContext())
        .title(R.string.tip_options)
        .items(R.array.menu_values)
        .itemsCallback((dialog, view, which, text) -> Toast(which + ": " + text))
        .show();
        
//使用DialogLoader可自由切换dialog显示的策略
DialogLoader.getInstance().showContextMenuDialog(...);         
  • 单选对话框

new MaterialDialog.Builder(getContext())
        .title(R.string.tip_capture_setting)
        .items(R.array.lead_name_choice_entry)
        .itemsCallbackSingleChoice(
                0,
                (dialog, view, which, text) -> {
                    Toast(which + ": " + text);
                    return true; // allow selection
                })
        .positiveText(R.string.lab_choice)
        .negativeText(R.string.lab_cancel)
        .show();
        
//使用DialogLoader可自由切换dialog显示的策略
DialogLoader.getInstance().showSingleChoiceDialog(...);         
  • 多选对话框

new MaterialDialog.Builder(getContext())
        .title(R.string.tip_capture_setting)
        .items(R.array.lead_name_choice_entry)
        .itemsCallbackMultiChoice(
               new Integer[]{0, 1},
                (dialog, which, text) -> {
                    StringBuilder sb = new StringBuilder("选中:\n");
                    for (int i = 0; i < which.length; i ++){
                        sb.append(which[i]).append(":").append(text[i]).append("\n");
                    }
                    Toast(sb.toString());
                    return true; // allow selection
                })
        .positiveText(R.string.lab_choice)
        .negativeText(R.string.lab_cancel)
        .show();
  • 水平有进度的进度条对话框

new MaterialDialog.Builder(getContext())
        .title(R.string.tip_update)
        .content(R.string.content_downloading)
        .contentGravity(GravityEnum.CENTER)
        .progress(false, 150, true)
        .cancelListener(
                dialog -> {
                    if (thread != null) {
                        thread.interrupt();
                    }
                })
        .showListener(
                dialogInterface -> {
                    updateProgress((MaterialDialog) dialogInterface);
                })
        .negativeText(R.string.lab_cancel)
        .show();

  • 环形无进度的进度条对话框

new MaterialDialog.Builder(getContext())
        .iconRes(R.drawable.icon_sex_man)
        .limitIconToDefaultSize()
        .title(R.string.tip_patient_name)
        .content(R.string.content_wait_for_receive_data)
        .progress(true, 0)
        .progressIndeterminateStyle(false)
        .negativeText(R.string.lab_cancel)
        .show();
  • 带图标条目的Dialog

List<MaterialSimpleListItem> list = new ArrayList<>();
list.add(new MaterialSimpleListItem.Builder(getContext())
        .content(R.string.lab_edit)
        .icon(R.drawable.icon_edit)
        .iconPaddingDp(8)
        .build());
list.add(new MaterialSimpleListItem.Builder(getContext())
        .content(R.string.lab_add)
        .icon(R.drawable.icon_add)
        .build());
list.add(new MaterialSimpleListItem.Builder(getContext())
        .content(R.string.lab_delete)
        .icon(R.drawable.icon_delete)
        .build());
list.add(new MaterialSimpleListItem.Builder(getContext())
        .content(R.string.lab_update)
        .icon(R.drawable.icon_update)
        .iconPaddingDp(8)
        .build());
final MaterialSimpleListAdapter adapter = new MaterialSimpleListAdapter(list)
        .setOnItemClickListener(new MaterialSimpleListAdapter.OnItemClickListener() {
            @Override
            public void onMaterialListItemSelected(MaterialDialog dialog, int index, MaterialSimpleListItem item) {
                ToastUtils.toast(item.getContent().toString());
            }
        });
new MaterialDialog.Builder(getContext()).adapter(adapter, null).show();
  • 自定义对话框

new MaterialDialog.Builder(getContext())
        .customView(R.layout.dialog_custom, true)
        .title("自定义对话框")
        .positiveText(R.string.lab_submit)
        .negativeText(R.string.lab_cancel)
        .show();
        
//或者继承CustomMaterialDialog实现

常用方法

  • 设置标题:title(CharSequence title)
  • 设置标题的对齐方式: titleGravity(GravityEnum gravity)
  • 设置标题字体的颜色:titleColor(int color)
  • 设置对话框的图标:icon(Drawable icon)
  • 设置对话框的内容:content(CharSequence content)
  • 设置对话框内容的文字颜色:contentColor(int color)
  • 设置对话框内容的对齐方式:contentGravity(GravityEnum gravity)
  • 设置对话框列表内容的集合:items(Collection collection)
  • 设置对话框列表的点击回调:itemsCallback(ListCallback callback)
  • 设置对话框列表的长按点击回调:itemsLongCallback( ListLongCallback callback)
  • 设置对话框列表字体的颜色:itemsColor(int color)
  • 设置对话框列表单选监听:itemsCallbackSingleChoice(int selectedIndex, ListCallbackSingleChoice callback)
  • 设置对话框列表多选监听:itemsCallbackMultiChoice(Integer[] selectedIndices, ListCallbackMultiChoice callback)
  • 设置确定按钮的文字:positiveText(CharSequence message)
  • 设置确定按钮的点击回调:onPositive(SingleButtonCallback callback)
  • 设置确定按钮文字的颜色:positiveColor(int color)
  • 设置中性按钮的文字:neutralText(CharSequence message)
  • 设置中性按钮的点击回调:onNeutral(SingleButtonCallback callback)
  • 设置否定按钮的文字:negativeText(CharSequence message)
  • 设置否定按钮的点击回调:onNegative(SingleButtonCallback callback)
  • 设置对话框列表的点击效果:listSelector(int selectorRes)
  • 设置按钮的点击效果:btnSelector(int selectorRes)
  • 设置对话框自定义布局:customView(int layoutRes, boolean wrapInScrollView)
  • 设置对话框进度条:progress(boolean indeterminate, int max)
  • 设置对话框点击外部是否可消失:cancelable(boolean cancelable)
  • 设置对话框按钮点击是否自动消失:autoDismiss(boolean dismiss)
  • 设置对话框的显示监听:showListener(OnShowListener listener)
  • 设置对话框的消失监听:dismissListener(OnDismissListener listener)
  • 设置对话框的取消监听:cancelListener(OnCancelListener listener)
  • 设置对话框输入框的样式:input(
CharSequence hint,
 CharSequence prefill,
 boolean allowEmptyInput,
 InputCallback callback)

DialogLoader

对话框加载者,可自定义对话框的加载策略。框架默认提供了两套加载策略:AlertDialogStrategyMaterialDialogStrategy。 默认使用的是MaterialDialogStrategy

设置加载的策略

DialogLoader.getInstance().setIDialogStrategy(...);

策略方法

  • 显示简要的提示对话框:showTipDialog
  • 显示简要的确认对话框:showConfirmDialog
  • 显示带输入框的对话框:showInputDialog
  • 显示类似上下文菜单的对话框:showContextMenuDialog
  • 显示带单选项的对话框:showSingleChoiceDialog

BottomSheet

底部弹出对话框,支持List和Grid两种形式。

List形式

new BottomSheet.BottomListSheetBuilder(getActivity())
        .addItem("Item 1")
        .addItem("Item 2")
        .addItem("Item 3")
        .setOnSheetItemClickListener(new BottomSheet.BottomListSheetBuilder.OnSheetItemClickListener() {
            @Override
            public void onClick(BottomSheet dialog, View itemView, int position, String tag) {
                dialog.dismiss();
                ToastUtils.toast("Item " + (position + 1));
            }
        })
        .build()
        .show();

Grid形式

BottomSheet.BottomGridSheetBuilder builder = new BottomSheet.BottomGridSheetBuilder(getActivity());
    builder
        .addItem(R.drawable.icon_more_operation_share_friend, "分享到微信", TAG_SHARE_WECHAT_FRIEND, BottomSheet.BottomGridSheetBuilder.FIRST_LINE)
        .addItem(R.drawable.icon_more_operation_share_moment, "分享到朋友圈", TAG_SHARE_WECHAT_MOMENT, BottomSheet.BottomGridSheetBuilder.FIRST_LINE)
        .addItem(R.drawable.icon_more_operation_share_weibo, "分享到微博", TAG_SHARE_WEIBO, BottomSheet.BottomGridSheetBuilder.FIRST_LINE)
        .addItem(R.drawable.icon_more_operation_share_chat, "分享到私信", TAG_SHARE_CHAT, BottomSheet.BottomGridSheetBuilder.FIRST_LINE)
        .addItem(R.drawable.icon_more_operation_save, "保存到本地", TAG_SHARE_LOCAL, BottomSheet.BottomGridSheetBuilder.SECOND_LINE)
        .setOnSheetItemClickListener(new BottomSheet.BottomGridSheetBuilder.OnSheetItemClickListener() {
            @Override
            public void onClick(BottomSheet dialog, BottomSheetItemView itemView) {
                dialog.dismiss();
                int tag = (int) itemView.getTag();
                ToastUtils.toast("tag:" + tag + ", content:" + itemView.toString());
            }
        }).build().show();

LoadingDialog

自定义的loading对话框,支持更新提示信息,修改图标配置,旋转速度等。

  • 演示效果

  • 使用案例
mLoadingDialog = WidgetUtils.getLoadingDialog(getContext())
            .setIconScale(0.4F)
            .setLoadingSpeed(8);

常用方法

  • 更新提示信息:updateMessage(String tipMessage)
  • 修改图标配置:setLoadingIcon(Drawable icon)
  • 资源回收:Recycle()
  • 设置loading旋转的速度:setLoadingSpeed(int speed)

MiniLoadingDialog

自定义的loading对话框,类似IOS的loading加载框,支持更新提示信息。

  • 演示效果

  • 使用案例
mMiniLoadingDialog =  WidgetUtils.getMiniLoadingDialog(getContext());