赞
踩
首先创建一个BaseDialog
public class BaseDialog extends CommonDialog { /** * 类似Android Dialog的cancelable属性, * true: 支持点击外部或返回键消失。 * false: 则只有手动调用hide才会消失。 */ private boolean cancelable = true; protected boolean hideWithBackPressed; private OnShowListener onShowListener; private OnDismissListener onHideListener; public BaseDialog(Context context) { super(context); } public boolean isCancelable() { return cancelable; } public void setCancelable(boolean cancelable) { this.cancelable = cancelable; if (!cancelable) { setAutoClosable(false); } } @Override public boolean clickKeyDown(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEY_BACK) { if (cancelable) { if (isShowing()) { hideWithBackPressed = true; destroy(); return true; } } else { return true; } } return super.clickKeyDown(event); } @Override protected void onShow() { super.onShow(); hideWithBackPressed = false; if (onShowListener != null) { onShowListener.onShow(this); } } @Override protected void onShowing() { super.onShowing(); } @Override protected void onHide() { super.onHide(); } @Override protected void onDestroy() { super.onDestroy(); if (onHideListener != null) { onHideListener.onDismiss(this, hideWithBackPressed); } } public void setOnShowListener(OnShowListener onShowListener) { this.onShowListener = onShowListener; } public void setOnDismissListener(OnDismissListener onHideListener) { this.onHideListener = onHideListener; } public interface OnShowListener { void onShow(IDialog dialog); } public interface OnDismissListener { void onDismiss(IDialog dialog, boolean withBackPressed); } }
布局
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent" ohos:orientation="vertical" ohos:id="$+id:dialog_Layout" > <DependentLayout ohos:height="match_content" ohos:width="match_parent" > <Text ohos:id="$+id:titlelog" ohos:width="match_content" ohos:height="match_content" ohos:horizontal_center="true" ohos:text="" ohos:multiple_lines="true" ohos:layout_alignment="center" ohos:text_alignment="center" ohos:left_margin="15vp" ohos:right_margin="15vp" ohos:top_margin="15vp" ohos:text_size="15fp"/> <Text ohos:id="$+id:title_content" ohos:height="match_content" ohos:width="match_content" ohos:horizontal_center="true" ohos:text_alignment="center" ohos:left_margin="30vp" ohos:right_margin="30vp" ohos:top_margin="40vp" ohos:layout_alignment="center" ohos:multiple_lines="true" ohos:text_color="#333333" ohos:text_size="14vp" /> <Component ohos:id="$+id:line" ohos:height="1px" ohos:width="match_parent" ohos:background_element="#A8A8A8" ohos:top_margin="30vp" ohos:below="$+id:title_content" /> <!-- ohos:top_margin="105vp" --> <StackLayout ohos:id="$+id:iscache_show" ohos:height="match_content" ohos:width="match_parent" ohos:layout_alignment="horizontal_center" ohos:below="$+id:line" ohos:visibility="hide" > <StackLayout ohos:height="40vp" ohos:width="match_parent" > <Button ohos:id="$+id:singledetermination" ohos:width="match_parent" ohos:text="知道了" ohos:text_size="18fp" ohos:layout_alignment="vertical_center" ohos:text_alignment="horizontal_center" ohos:height="match_content"/> </StackLayout> </StackLayout> <StackLayout ohos:id="$+id:isstyle" ohos:height="match_content" ohos:width="match_parent" ohos:layout_alignment="horizontal_center" ohos:below="$+id:line" ohos:visibility="visible" > <Text ohos:height="match_content" ohos:width="1vp" ohos:layout_alignment="horizontal_center" ohos:text_color="#A8A8A8" ohos:text="|" /> <Text ohos:height="match_content" ohos:width="1vp" ohos:layout_alignment="horizontal_center" ohos:text_color="#A8A8A8" ohos:text="|" ohos:top_margin="7vp" /> <Text ohos:height="match_content" ohos:width="1vp" ohos:layout_alignment="horizontal_center" ohos:text_color="#A8A8A8" ohos:text="|" ohos:top_margin="14vp" /> <Text ohos:height="match_content" ohos:width="1vp" ohos:layout_alignment="horizontal_center" ohos:text_color="#A8A8A8" ohos:text="|" ohos:top_margin="21vp" /> <Text ohos:height="match_content" ohos:width="1vp" ohos:layout_alignment="horizontal_center" ohos:text_color="#A8A8A8" ohos:text="|" ohos:top_margin="28vp" /> <Text ohos:height="match_content" ohos:width="1vp" ohos:layout_alignment="horizontal_center" ohos:text_color="#A8A8A8" ohos:text="|" ohos:top_margin="35vp" /> <DependentLayout ohos:height="match_content" ohos:width="match_parent" ohos:orientation="horizontal" > <StackLayout ohos:height="40vp" ohos:width="140vp" ohos:align_parent_right="true" > <Button ohos:id="$+id:determine" ohos:width="match_parent" ohos:text="" ohos:text_size="18fp" ohos:top_margin="10vp" ohos:height="match_content"/> </StackLayout> <StackLayout ohos:height="40vp" ohos:width="140vp" ohos:align_parent_left="true"> <Button ohos:id="$+id:cancel" ohos:width="match_parent" ohos:text="" ohos:text_size="18fp" ohos:top_margin="10vp" ohos:height="match_content"/> </StackLayout> <DependentLayout ohos:id="$+id:determine_max" ohos:height="48vp" ohos:width="143vp" ohos:align_parent_right="true"/> <DependentLayout ohos:id="$+id:cancel_max" ohos:height="48vp" ohos:width="143vp" ohos:align_parent_left="true"/> </DependentLayout> </StackLayout> </DependentLayout> </DirectionalLayout>
然后就是正题DialogUtils
public class DialogUtils { /* * 多种样式Dialog * */ private static final int PROMPT_DIALOG_STYLE_1_WITH_TITLE = 1; private static final String BUTTONCOLOR = "#FF9900";//按钮颜色默认橙色 private static final int DIALOG_CORNER = 35; private Builder mBuilder; private DialogUtils(Builder builder) { this.mBuilder = builder; } public void show() { mBuilder.showDialog(); } public boolean isShowing() { return mBuilder.isShowing(); } public static class Builder implements Component.ClickedListener { private Button cancel_dialog, determine_dialog; private Text titlelog; private Text title_content; private BaseDialog commonDialog; private DialogListener mDialogListener; private String title; private String info; private String strLeftBtn; private String strRightBtn; private int mType = 1; private String leftbutton = BUTTONCOLOR;//设置左边按钮的颜色 private String rightbutton = BUTTONCOLOR;//设置右边按钮的颜色 private boolean isbuttoncolor = true; private String titlecontent; private int isBtnType = 0; private DependentLayout determine_max; private DependentLayout cancel_max; private final Component component; private StackLayout iscache_show; private StackLayout isstyle; private Button singledetermination; private StackLayout ds; private boolean cancelable = true; private DirectionalLayout dialog_layout; public interface DialogListener { void ok(); void cancel(); } void showDialog() { initViewStatus(); commonDialog.show(); } boolean isShowing() { return commonDialog.isShowing(); } public Builder(Context context) { commonDialog = new BaseDialog(context); component = LayoutScatter.getInstance(context) .parse(ResourceTable.Layout_DialogUtils_layout, null, false); dialog_layout = (DirectionalLayout) component.findComponentById(ResourceTable.Id_dialog_Layout); dialog_layout.setComponentSize(850,DirectionalLayout.LayoutConfig.MATCH_CONTENT);//设置layout大小 commonDialog.setSize(850, DirectionalLayout.LayoutConfig.MATCH_CONTENT);//宽与高 commonDialog.setAlignment(LayoutAlignment.HORIZONTAL_CENTER); commonDialog.setAlignment(LayoutAlignment.CENTER); commonDialog.setCornerRadius(DIALOG_CORNER); commonDialog.setContentCustomComponent(component); commonDialog.setCancelable(cancelable); initUI(component); initEvent(); } private void initUI(Component component) {//初始化布局 determine_dialog = (Button) component.findComponentById(ResourceTable.Id_determine); cancel_dialog = (Button) component.findComponentById(ResourceTable.Id_cancel); titlelog = (Text) component.findComponentById(ResourceTable.Id_titlelog); title_content = (Text) component.findComponentById(ResourceTable.Id_title_content);//Dialog设置内容 determine_max = (DependentLayout) component.findComponentById(ResourceTable.Id_determine_max); cancel_max = (DependentLayout) component.findComponentById(ResourceTable.Id_cancel_max); iscache_show = (StackLayout) component.findComponentById(ResourceTable.Id_iscache_show);//是否显示缓存样式Dialog(只有确定) isstyle = (StackLayout) component.findComponentById(ResourceTable.Id_isstyle);//是否变换样式 singledetermination = (Button) component.findComponentById(ResourceTable.Id_singledetermination);//单个确定 } private void initEvent() { determine_max.setClickedListener(this); cancel_max.setClickedListener(this); iscache_show.setClickedListener(this); singledetermination.setClickedListener(this); determine_dialog.setClickedListener(this); cancel_dialog.setClickedListener(this); } private void initViewStatus() { if (PROMPT_DIALOG_STYLE_1_WITH_TITLE == mType) { if (!TextUtils.isEmpty(title)) { titlelog.setText(title); titlelog.setAutoFontSizeRule(40, 50, 1);//自动换行改变大小 } } if (!TextUtils.isEmpty(strLeftBtn)) { determine_dialog.setText(strLeftBtn); singledetermination.setText(strLeftBtn);//给单个确定设置text if (isbuttoncolor) {//给btn添加颜色 determine_dialog.setTextColor(new Color(Color.getIntColor(leftbutton))); singledetermination.setTextColor(new Color(Color.getIntColor(leftbutton))); } isstyle.setVisibility(Component.VISIBLE); iscache_show.setVisibility(Component.HIDE); } else { isstyle.setVisibility(Component.HIDE); iscache_show.setVisibility(Component.VISIBLE); singledetermination.setTextColor(new Color(Color.getIntColor(rightbutton))); } if (!TextUtils.isEmpty(strRightBtn)) { cancel_dialog.setText(strRightBtn); singledetermination.setText(strRightBtn);//给单个确定设置text左右缺一默认单一按钮 if (isbuttoncolor && isBtnType == 1) {//给btn添加颜色 cancel_dialog.setTextColor(new Color(Color.getIntColor(rightbutton))); singledetermination.setTextColor(new Color(Color.getIntColor(rightbutton))); } isstyle.setVisibility(Component.VISIBLE); iscache_show.setVisibility(Component.HIDE); } else { isstyle.setVisibility(Component.HIDE); iscache_show.setVisibility(Component.VISIBLE); } if (!TextUtils.isEmpty(titlecontent)) { title_content.setText(titlecontent); title_content.setTextSize(14, Text.TextSizeType.VP); } } @Override public void onClick(Component component) { if (component == determine_max) { commonDialog.destroy(); mDialogListener.ok(); } if (component == cancel_max) { commonDialog.destroy(); mDialogListener.cancel(); } if (component == cancel_dialog) { commonDialog.destroy(); mDialogListener.cancel(); } if (component == determine_dialog) { commonDialog.destroy(); mDialogListener.ok(); } if (component == singledetermination) {//单一确定按钮无取消 commonDialog.destroy(); mDialogListener.ok(); } } public Builder setListener(DialogListener listener) { this.mDialogListener = listener; return this; } public Builder setLeftButtonColor(String leftbutton) {//左边按钮的颜色 this.leftbutton = leftbutton; return this; } public Builder setRightButtonColor(String rightbutton) {//右边按钮的颜色 this.rightbutton = rightbutton; return this; } public Builder setTitleContent(String titlecontent) { this.titlecontent = titlecontent; return this; } public Builder setTitle(String title) { this.title = title; return this; } public Builder setLeftButtonTitle(String title) { this.strLeftBtn = title; return this; } public Builder setRightButtonTitle(String title) { this.strRightBtn = title; return this; } public Builder setmType(int mType) { this.mType = mType; return this; } public Builder isBtnType(int isBtnType) { this.isBtnType = isBtnType; return this; } public Builder isbuttoncolor(boolean isbuttoncolor) { this.isbuttoncolor = isbuttoncolor; return this; } public Builder setCancelable(boolean cancelable) { this.cancelable = cancelable; return this; } public WeiboDialog build() { return new WeiboDialog(this); } } }
使用方法
String content = "我是一个菜鸟工程师哦!"; DialogUtils dialog = new DialogUtils.Builder(MainAbilitySlice.this) .setCancelable(true) .setRightButtonTitle("取消") .setLeftButtonTitle("确定") .setTitleContent(content) .setListener(new DialogUtils.Builder.DialogListener() { @Override public void ok() { //删除微博 } @Override public void cancel() { } }) .build(); dialog.show();
使用方法
类型1关注类型
String content = "我是一个菜鸟工程师哦!"; DialogUtils dialog = new DialogUtils.Builder(MainAbilitySlice.this) .setCancelable(true) .setRightButtonTitle("取消") .setLeftButtonTitle("确定") .setTitleContent(content) .setListener(new DialogUtils.Builder.DialogListener() { @Override public void ok() { //删除微博 } @Override public void cancel() { } }) .build(); dialog.show();
效果
类型2权限访问类型
WeiboDialog dialogUtils = new WeiboDialog.Builder(MainAbilitySlice.this) .setListener(new WeiboDialog.Builder.DialogListener() { @Override public void ok() { } @Override public void cancel() { } }) .setmType(1) .setTitle("应用想访问您的照片") .isbuttoncolor(true) .setTitleContent("如果不允许您将无法保存到相册,也无法分享到本软件") .setLeftButtonColor("#0066CC") .setRightButtonColor("#0066CC") .isBtnType(1) .setRightButtonTitle("不允许") .setLeftButtonTitle("好") .build(); dialogUtils.show();
效果
还有其他类型根据需求自己定义
提供一个text工具
public class TextUtils { public static boolean isEmpty(CharSequence str) { return str == null || str.length() == 0; } public static boolean equals(CharSequence a, CharSequence b) { if (a == b) return true; int length; if (a != null && b != null && (length = a.length()) == b.length()) { if (a instanceof String && b instanceof String) { return a.equals(b); } else { for (int i = 0; i < length; i++) { if (a.charAt(i) != b.charAt(i)) return false; } return true; } } return false; } public static String toString(String value){ if(isEmpty(value)){ return ""; } if(isEmpty(value.trim())){ return ""; } return value; } /** * Returns whether the given CharSequence contains only digits. */ public static boolean isDigitsOnly(CharSequence str) { final int len = str.length(); for (int cp, i = 0; i < len; i += Character.charCount(cp)) { cp = Character.codePointAt(str, i); if (!Character.isDigit(cp)) { return false; } } return true; } public static int measureText(Paint p, String text) { if (p == null || text == null) return 0; float[] charWidth = new float[text.length()]; p.getAdvanceWidths(text, charWidth); float totalWidth = 0; for(float w : charWidth) { totalWidth += w; } return (int) totalWidth; } public static boolean endsWith(String str, String end) { return !isEmpty(str) && str.endsWith(end); } }
支持鸿蒙
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。