39 Star 659 Fork 206

桃花镇童长老/harmony-utils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
DialogPage.ets 45.19 KB
一键复制 编辑 原始数据 按行查看 历史
tongyy 提交于 13天前 . 代码优化。
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
import { AreaHelper } from '@nutpi/china_area';
import { AnimationHelper, DateType, DialogAction, DialogHelper, Orientation } from '@pura/harmony-dialog';
import { DateUtil, DisplayUtil, LogUtil, ToastUtil } from '@pura/harmony-utils';
import { SpinType } from '@pura/spinkit';
import { CardButton } from '../../component/CardButton';
import { CustomImageView } from '../../component/CustomImageView';
import { CustomTextViewBuilder } from '../../component/CustomTextView';
import { DrawerBuilder } from '../../component/DrawerLayout';
import { LottieBuilder } from '../../component/LottieView';
import { MenuBuilder } from '../../component/MenuLayout';
import { MenuSheetBuilder } from '../../component/MenuSheetLayout';
import { TipBuilder } from '../../component/TipLayout';
import { TitleView } from '../../component/TitleView';
import { DrawerOptions } from '../../model/DrawerOptions';
import { LoadOptions } from '../../model/LoadOptions';
import { MenuOptions } from '../../model/MenuOptions';
import { MenuSheetOptions } from '../../model/MenuSheetOptions';
import { TipOptions } from '../../model/TipOptions';
import { Utils } from '../../utils/Utils';
import { LevelMode, router } from '@kit.ArkUI';
/**
* TODO 弹窗工具
* author: 桃花镇童长老ᥫ᭡
* since: 2024/08/18
*/
@Preview
@Component
export struct DialogPage {
private scroller: Scroller = new Scroller();
@State menuArray: Array<ResourceStr> = [$r('app.string.str_camera'), $r('app.string.str_album'), "文件管理器"];
@State inputText: string = ""; //输入框内容
@State progress: number = 1;
private likes: string[] = ['乒乓球', '鸿蒙手机', 'ArkTS', 'Mate70Pro', '三折叠手机', '小米su7', '红米K80pro', '广东名菜烤乳猪'];
private apfruits: string[] = ['apple1', 'apple2', 'apple3', 'apple4']
private orfruits: string[] = ['orange1', 'orange2', 'orange3', 'orange4']
private pefruits: string[] = ['peach1', 'peach2', 'peach3', 'peach4']
private multis: string[][] = [this.apfruits, this.orfruits, this.pefruits]
@BuilderParam customTextBuilderParam: (str: string | Resource) => void = CustomTextViewBuilder; //自定义文本内容
private rangeContent: TextPickerRangeContent[] = [{
icon: $r('sys.media.ohos_ic_normal_white_grid_doc'),
text: 'DOC'
}, {
icon: $r('sys.media.ohos_ic_normal_white_grid_pdf'),
text: 'PDF'
}, {
icon: $r('sys.media.ohos_ic_normal_white_grid_xls'),
text: 'XLS'
}, {
icon: $r('sys.media.ohos_ic_normal_white_grid_image'),
text: 'IMAGE'
}, {
icon: $r('sys.media.ohos_ic_normal_white_grid_html'),
text: 'HTML'
}];
/**
* 统一配置一
*/
customConfig1() {
DialogHelper.setDefaultConfig((config) => {
config.autoCancel = false; //点击遮障层时,是否关闭弹窗,true表示关闭弹窗。
config.backCancel = false; //点击返回键或手势返回时,是否关闭弹窗;实现onWillDismiss函数时,该参数不起作用。
config.actionCancel = true; //点击操作按钮时,是否关闭弹窗。false表示不关闭弹窗。
config.alignment = DialogAlignment.Center; //弹窗的对齐方式。
config.offset = { dx: 0, dy: 0 }; //弹窗相对alignment所在位置的偏移量。默认值:{ dx: 0, dy: 0 }
config.maskColor = 0x11000000; //自定义蒙层颜色。默认值 0x33000000
config.backgroundColor = Color.White; //弹窗背板颜色。默认值:Color.White
config.backgroundBlurStyle = BlurStyle.NONE; //弹窗背板模糊材质
config.cornerRadius = 35; //设置背板的圆角半径。可分别设置4个圆角的半径。
// config.maskRect={ x: 'auto', y: 'auto', width: 'auto', height: 'auto' };
config.title = $r("app.string.dialog_tip"); //弹框标题
config.primaryButton = $r("app.string.btn_cancel"); //弹框左侧按钮。
config.secondaryButton = $r("app.string.btn_sure"); //弹框右侧按钮。
config.imageRes = undefined; //TipsDialog用到,展示的图片。
config.imageSize = { width: '64vp', height: '64vp' }; //TipsDialog用到,自定义图片尺寸。默认值:64*64vp
config.loading_loadSize = 60; //加载动画或进度条的大小
config.loading_loadColor = Color.White; //加载动画或进度条的颜色
config.loading_content = ''; //加载动画的提示文字
config.loading_fontSize = 16; //文字大小
config.loading_fontColor = Color.White; //文字颜色
config.loading_backgroundColor = '#CC094CD1'; //背景颜色,八位色值前两位为透明度
config.loading_borderRadius = 10; //背景圆角
config.toast_fontSize = 16; //文字大小
config.toast_fontColor = Color.White; //文字颜色
config.toast_backgroundColor = '#CC56D866'; //背景颜色,建议八位色值前两位为透明度
config.toast_alignment = Alignment.Center; //土司居中显示
config.toast_borderRadius = 8; //背景圆角
config.toast_padding = { left: 16, right: 16, top: 12, bottom: 12 }; //Padding
config.toast_imageSize = { width: 45, height: 45 }; //Tip图片尺寸。默认值:45*45vp
config.toast_duration = 1500; //显示时长(1500ms-10000ms)
config.toast_durationLong = 5000; //显示时长
});
ToastUtil.showToast("统一配置设置成功!");
}
/**
* 统一配置二
*/
customConfig2() {
DialogHelper.setDefaultConfig((config) => {
config.autoCancel = false; //点击遮障层时,是否关闭弹窗,true表示关闭弹窗。
config.backCancel = false; //点击返回键或手势返回时,是否关闭弹窗;实现onWillDismiss函数时,该参数不起作用。
config.actionCancel = true; //点击操作按钮时,是否关闭弹窗。false表示不关闭弹窗。
config.alignment = DialogAlignment.Bottom; //弹窗的对齐方式。
config.offset = { dx: 0, dy: -35 }; //弹窗相对alignment所在位置的偏移量。
config.maskColor = 0x11000000; //自定义蒙层颜色。默认值 0x33000000
config.backgroundColor = Color.White; //弹窗背板颜色。默认值:Color.White
config.backgroundBlurStyle = BlurStyle.COMPONENT_ULTRA_THICK; //弹窗背板模糊材质
config.cornerRadius = 20; //设置背板的圆角半径。可分别设置4个圆角的半径。
config.title = '提示'; //弹框标题
config.primaryButton = '取消'; //弹框左侧按钮。
config.secondaryButton = '确定'; //弹框右侧按钮。
config.imageRes = undefined; //TipsDialog用到,展示的图片。
config.imageSize = { width: '64vp', height: '64vp' }; //TipsDialog用到,自定义图片尺寸。
config.loading_loadSize = 60; //加载动画或进度条的大小
config.loading_loadColor = Color.White; //加载动画或进度条的颜色
config.loading_content = ''; //加载动画的提示文字
config.loading_fontSize = 16; //文字大小
config.loading_fontColor = Color.White; //文字颜色
config.loading_backgroundColor = '#CC56D866'; //背景颜色,八位色值前两位为透明度
config.loading_borderRadius = 10; //背景圆角
config.toast_fontSize = 16; //文字大小
config.toast_fontColor = Color.Black; //文字颜色
config.toast_backgroundColor = '#CC0FCEE3'; //背景颜色,建议八位色值前两位为透明度
config.toast_alignment = Alignment.Top; //土司Top显示
config.toast_borderRadius = 8; //背景圆角
config.toast_padding = { left: 16, right: 16, top: 12, bottom: 12 }; //Padding
config.toast_imageSize = { width: 45, height: 45 }; //Tip图片尺寸。默认值:45*45vp
config.toast_duration = 1500; //显示时长(1500ms-10000ms)
config.toast_durationLong = 5000; //显示时长
});
ToastUtil.showToast("统二配置设置成功!");
}
/**
* 默认的配置
*/
defaultConfig() {
DialogHelper.setDefaultConfig((config) => {
config.autoCancel = true; //点击遮障层时,是否关闭弹窗,true表示关闭弹窗。false表示不关闭弹窗。默认值:true
config.backCancel = true; //点击返回键或手势返回时,是否关闭弹窗;实现onWillDismiss函数时,该参数不起作用。true表示关闭弹窗。false表示不关闭弹窗。默认值:true。
config.actionCancel = true; //点击操作按钮时,是否关闭弹窗。false表示不关闭弹窗。默认值:true。
config.alignment = DialogAlignment.Default; //弹窗的对齐方式。
config.offset = { dx: 0, dy: 0 }; //弹窗相对alignment所在位置的偏移量。默认值:{ dx: 0, dy: 0 }
config.maskColor = 0x33000000; //自定义蒙层颜色。默认值 0x33000000
config.backgroundColor = Color.White; //弹窗背板颜色。默认值:Color.White
config.backgroundBlurStyle = BlurStyle.COMPONENT_ULTRA_THICK; //弹窗背板模糊材质。默认值:BlurStyle.COMPONENT_ULTRA_THICK
config.cornerRadius = 20; //设置背板的圆角半径。可分别设置4个圆角的半径。
config.title = '温馨提示'; //弹框标题
config.primaryButton = '取消'; //弹框左侧按钮。
config.secondaryButton = '确定'; //弹框右侧按钮。
config.imageRes = undefined; //TipsDialog用到,展示的图片。
config.imageSize = { width: '64vp', height: '64vp' }; //TipsDialog用到,自定义图片尺寸。默认值:64*64vp
config.loading_loadSize = 60; //加载动画或进度条的大小
config.loading_loadColor = Color.White; //加载动画或进度条的颜色
config.loading_content = ''; //加载动画的提示文字
config.loading_fontSize = 16; //文字大小
config.loading_fontColor = Color.White; //文字颜色
config.loading_backgroundColor = '#CC000000'; //背景颜色,八位色值前两位为透明度
config.loading_borderRadius = 10; //背景圆角
config.toast_fontSize = 16; //文字大小
config.toast_fontColor = Color.White; //文字颜色
config.toast_backgroundColor = '#CC000000'; //背景颜色,建议八位色值前两位为透明度
config.toast_alignment = undefined; //土司显示位置
config.toast_borderRadius = 8; //背景圆角
config.toast_padding = { left: 16, right: 16, top: 12, bottom: 12 }; //Padding
config.toast_imageSize = { width: 45, height: 45 }; //Tip图片尺寸。默认值:45*45vp
config.toast_duration = 2000; //显示时长(1500ms-10000ms)
config.toast_durationLong = 10000; //显示时长(10000ms)
});
ToastUtil.showToast("默认配置,设置成功!");
}
/**
* 操作确认类弹出框
*/
alertDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showAlertDialog({
uiContext:this.getUIContext(),
backgroundColor: Color.Orange,
content: "确定保存该WPS文件吗?",
onAction: (action) => {
if (action == DialogAction.CANCEL) {
ToastUtil.showToast(`您点击了取消按钮`);
} else if (action == DialogAction.SURE) {
ToastUtil.showToast(`您点击了确认按钮`);
}
}
})
} else if (index == 1) {
//自定义按钮颜色
DialogHelper.showAlertDialog({
title: "提示",
content: "确定保存该视频文件吗?",
// primaryButton:'取消',
secondaryButton: {
value: "确认",
fontColor: Color.Brown
},
onAction: (action) => {
if (action == DialogAction.CANCEL) {
ToastUtil.showToast(`您取消了保存`);
} else if (action == DialogAction.SURE) {
ToastUtil.showToast(`已保存`);
}
}
})
} else if (index == 2) {
//自定义按钮和二级标题
DialogHelper.showAlertDialog({
primaryTitle: "提示",
secondaryTitle: "保存前请确认文件",
content: "确定保存该视频文件吗",
primaryButton: "取消",
secondaryButton: $r('app.string.btn_save'),
onAction: (action) => {
if (action == DialogAction.CANCEL) {
ToastUtil.showToast(`您取消了保存`);
} else if (action == DialogAction.SURE) {
ToastUtil.showToast(`已保存`);
}
}
})
} else {
//不带标题,不主动关闭弹框
DialogHelper.showAlertDialog({
title: "",
content: "确定保存该视频文件吗",
actionCancel: false, //点击操作按钮不关闭弹框
autoCancel: false, //点击遮障层时,不关闭弹窗
backCancel: false, //点击返回键,不关闭弹窗
onAction: (action, dialogId) => {
DialogHelper.closeDialog(dialogId); //关闭弹框
if (action == DialogAction.CANCEL) {
ToastUtil.showToast(`您取消了保存`);
} else if (action == DialogAction.SURE) {
ToastUtil.showToast(`已保存`);
}
}
})
}
}
/**
* 信息确认类弹出框
*/
confirmDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showConfirmDialog({
checkTips: "是否记住密码?",
content: "您是否退出当前应用",
onCheckedChange: (check) => {
ToastUtil.showToast(`${check}`);
},
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else if (index == 1) {
//自定义按钮
DialogHelper.showConfirmDialog({
checkTips: "是否记住密码?",
content: "您是否退出当前应用",
primaryButton: { value: "取消", fontColor: Color.Red },
secondaryButton: "退出",
onCheckedChange: (check) => {
ToastUtil.showToast(`${check}`);
},
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else if (index == 2) {
//不显示标题
DialogHelper.showConfirmDialog({
title: "",
checkTips: "是否记住密码?",
content: "您是否退出当前应用",
onCheckedChange: (check) => {
ToastUtil.showToast(`${check}`);
},
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else {
//底部弹出
DialogHelper.showConfirmDialog({
checkTips: "是否记住密码?",
content: "您是否退出当前应用",
alignment: DialogAlignment.Bottom, //底部弹出
offset: { dx: 0, dy: -35 },
autoCancel: false, //点击遮障层时,不关闭弹窗
onCheckedChange: (check) => {
ToastUtil.showToast(`${check}`);
},
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
}
}
/**
* 提示弹出框
*/
tipsDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showTipsDialog({
uiContext:this.getUIContext(),
content: '想要卸载这个APP嘛?',
showInSubWindow:false,
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else if (index == 1) {
//自定义图标和图标大小
DialogHelper.showTipsDialog({
imageRes: $r('sys.media.ohos_app_icon'),
imageSize: { width: 50, height: 50 },
content: '想要卸载这个APP嘛?',
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else if (index == 2) {
//自定义图标和按钮
DialogHelper.showTipsDialog({
imageRes: $r('app.media.icon_tip_warn'),
content: '想要卸载这个APP嘛?',
primaryButton: "卸载",
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else {
//自定义
DialogHelper.showTipsDialog({
imageRes: $r('sys.media.ohos_app_icon'),
imageSize: { width: 50, height: 50 },
content: '想要卸载这个APP嘛?',
checkTips: "是否删除缓存文件",
onCheckedChange: (check) => {
ToastUtil.showToast(`${check}`);
},
primaryButton: {
value: "取消",
fontColor: Color.Black
},
secondaryButton: {
value: "卸载",
fontColor: Color.Red
},
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
}
}
/**
* 选择类弹出框
*/
selectDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showSelectDialog({
radioContent: ["文本菜单选项一", "文本菜单选项二", "文本菜单选项三", "文本菜单选项四", "文本菜单选项五"],
onCheckedChanged: (index) => {
ToastUtil.showToast(`${index}`);
},
onAction: (action, dialogId, value) => {
ToastUtil.showToast(`${action} --- ${value}`);
}
})
} else if (index == 1) {
//自定义标题,内容,底部弹出框
let dialogID = DialogHelper.showSelectDialog({
title: "请选择",
content: "选择您中意的文本菜单",
alignment: DialogAlignment.Bottom, //底部弹出
offset: { dx: 0, dy: -35 },
autoCancel: false, //点击遮障层时,不关闭弹窗
radioContent: ["文本菜单选项一", "文本菜单选项二", "文本菜单选项三", "文本菜单选项四", "文本菜单选项五"],
onCheckedChanged: (index) => {
ToastUtil.showToast(`${index}`);
DialogHelper.closeDialog(dialogID);
},
onAction: (action, dialogId, value) => {
ToastUtil.showToast(`${action} --- ${value}`);
DialogHelper.closeDialog(dialogId);
}
})
} else if (index == 2) {
//自定义标题,内容,底部弹出框
DialogHelper.showSelectDialog({
title: "请选择菜单",
content: "选择您中意的文本菜单",
selectedIndex: 3, //设置选中索引
autoCancel: false, //点击遮障层时,不关闭弹窗
radioContent: [{
title: "文本菜单选项一",
icon: $r('app.media.icon_tip_warn'),
action: () => {
}
},
{
title: "文本菜单选项二",
icon: $r('sys.media.ohos_ic_public_voice'),
action: () => {
}
},
{
title: "文本菜单选项三",
icon: $r('sys.media.ohos_ic_public_voice'),
action: () => {
}
},
{
title: "文本菜单选项四",
icon: $r('sys.media.ohos_ic_public_voice'),
action: () => {
}
},
{
title: "文本菜单选项五",
icon: $r('sys.media.ohos_ic_public_voice'),
action: () => {
}
}],
onCheckedChanged: (index) => {
ToastUtil.showToast(`${index}`);
},
onAction: (action, dialogId, value) => {
ToastUtil.showToast(`${action} --- ${value}`);
}
})
}
}
/**
* 单行文本输入弹出框
*/
textInputDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showTextInputDialog({
autoCancel: false,
backCancel: false,
actionCancel: false,
text: this.inputText,
onAction: (action, dialogId, content) => {
if (action == DialogAction.SURE) {
this.inputText = content;
ToastUtil.showToast(content);
}
DialogHelper.closeDialog(dialogId);
}
})
} else if (index == 1) {
//自定义按钮、标题、边距等
DialogHelper.showTextInputDialog({
title: "支付密码",
placeholder: "请输入密码",
inputType: InputType.Password,
showPassword: false,
showPasswordIcon: true,
passwordIcon: {
onIconSrc: $r('sys.media.ohos_ic_public_voice'),
offIconSrc: $r('sys.media.ohos_ic_public_voice_off')
},
passwordRules:"1234567",
cancelButton: {
style: CancelButtonStyle.INPUT
},
defaultFocus: true, //主动拉起键盘
text: this.inputText,
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: -35 },
contentAreaPadding: { top: 12, bottom: 16, left: 15, right: 15 },
buttons: [
{ value: "取消", fontColor: Color.Red },
"确认"
],
onChange: (text) => {
console.error("onChange: " + text);
},
onAction: (action, dialogId, content) => {
this.inputText = content;
}
})
} else if (index == 2) {
//不带标题的
DialogHelper.showTextInputDialog({
title: "",
buttons: [
'取消',
{ value: '保存', fontColor: Color.Red }
],
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: -35 },
contentAreaPadding: { top: 30, bottom: 10, left: 15, right: 15 },
text: this.inputText,
placeholder: "请输入人员年龄",
inputType: InputType.Number,
maxLength: 3,
inputFilter: {
value: "[0-9]",
error: (value) => {
console.error("inputFilter: " + value);
}
},
onChange: (text) => {
console.error("onChange: " + text);
},
onAction: (action, dialogId, content) => {
if (action == DialogAction.SURE) {
this.inputText = content;
ToastUtil.showToast(content);
} else if (action == DialogAction.CANCEL) {
}
}
})
}
}
/**
* 多行文本输入弹出框
*/
textAreaDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showTextAreaDialog({
text: this.inputText,
onChange: (text) => {
console.error("onChange: " + text);
},
onAction: (action, dialogId, content) => {
if (action == DialogAction.SURE) {
this.inputText = content;
ToastUtil.showToast(content);
}
}
})
} else if (index == 1) {
//自定义弹框高度
DialogHelper.showTextAreaDialog({
title: "评论内容",
text: this.inputText,
defaultFocus: true,
placeholder: "请输入,[a-z,A-Z,0-9],长度20",
maxLength: 20,
inputFilter: {
value: "[0-9A-Za-z]",
error: (value) => {
LogUtil.error("inputFilter: " + value);
}
},
inputHeight: 160,
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: -35 },
buttons: [{
value: "确认",
fontColor: Color.Red
}],
autoCancel: false,
onChange: (text) => {
console.error("onChange: " + text);
},
onAction: (action, dialogId, content) => {
this.inputText = content;
}
})
} else if (index == 2) {
//自定义弹框,多按钮,
DialogHelper.showTextAreaDialog({
primaryTitle: "批示操作弹框",
secondaryTitle: "请填写批示意见",
text: this.inputText,
buttons: ["暂存", "提交"],
autoCancel: false,
onChange: (text) => {
console.error("onChange: " + text);
},
onAction: (action, dialogId, content) => {
if (action == DialogAction.SURE) {
this.inputText = content;
ToastUtil.showToast(content);
}
}
})
}
}
/**
* 自定义内容区弹出框
*/
customContentDialog(index: number) {
if (index == 0) {
//简单使用,局部 @Builder
DialogHelper.showCustomContentDialog({
contentBuilder: () => {
this.customTextBuilder("我是一个自定义文本内的弹框!")
},
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else if (index == 1) {
//自定义文本内容,使用全局 @Builder 和 自定义组件
DialogHelper.showCustomContentDialog({
title: "哈哈哈哈哈哈",
contentBuilder: () => {
this.customTextBuilderParam($r('app.string.module_desc'))
},
buttons: [
'取消',
{ value: '知道了', fontColor: Color.Red }
],
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
} else if (index == 2) {
//自定义图文,使用局部 @Builder 和 自定义组件
DialogHelper.showCustomContentDialog({
primaryTitle: "爱心提示",
secondaryTitle: "其实我是一个二级标题",
contentBuilder: () => {
this.customTextImgBuilder("最受欢迎的三方库,@pura/harmony-utils",$r('app.media.test_as2'))
},
buttons: [
{ value: '取消', fontColor: Color.Black },
{ value: '知道了', fontColor: Color.Red }
],
onAction: (action) => {
ToastUtil.showToast(`${action}`);
}
})
}
}
/**
* 动作面板
*/
bottomSheetDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showBottomSheetDialog({
sheets: this.menuArray,
onAction: (index) => {
ToastUtil.showToast(`您点击了,${this.menuArray[index]}`);
}
})
} else if (index == 1) {
//带标题
DialogHelper.showBottomSheetDialog({
title: "请选择上传方式",
sheets: this.menuArray,
onAction: (index) => {
ToastUtil.showToast(`您点击了,${this.menuArray[index]}`);
}
})
} else if (index == 2) {
//自定义按钮
DialogHelper.showBottomSheetDialog({
title: "请选择操作方式",
sheets: [{
value: "文本操作选项一",
fontColor: Color.Red,
fontSize: 18
}, {
value: "文本操作选项二",
fontColor: Color.Blue,
fontSize: 18
}, {
value: "文本操作选项三",
fontColor: Color.Brown,
fontSize: 18
}, {
value: "文本操作选项四",
fontColor: Color.Green,
fontSize: 18
}],
onAction: (index) => {
ToastUtil.showToast(`您点击了第几个按钮,${index + 1}`);
}
})
}
}
/**
* 动作面板(IOS风格)
*/
actionSheetDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showActionSheetDialog({
actionCancel: false,
sheets: this.menuArray,
onAction: (index) => {
ToastUtil.showToast(`您点击了,${this.menuArray[index]}`);
}
})
} else if (index == 1) {
//带标题
DialogHelper.showActionSheetDialog({
title: "请选择上传方式",
cancelFontColor: Color.Red,
cancelFontWeight: FontWeight.Normal,
sheets: this.menuArray,
onAction: (index) => {
ToastUtil.showToast(`您点击了,${this.menuArray[index]}`);
}
})
} else if (index == 2) {
//自定义按钮
DialogHelper.showActionSheetDialog({
title: "请选择操作方式",
sheets: [{
value: "文本操作选项一",
fontColor: Color.Red,
fontSize: 18
}, {
value: "文本操作选项二",
fontColor: Color.Blue,
fontSize: 18
}, {
value: "文本操作选项三",
fontColor: Color.Brown,
fontSize: 18
}, {
value: "文本操作选项四",
fontColor: Color.Green,
fontSize: 18
}],
onAction: (index) => {
ToastUtil.showToast(`您点击了第几个按钮,${index + 1}`);
},
onDidDisappear: () => {
ToastUtil.showToast("ActionSheetDialog关闭了");
}
})
}
}
/**
* TextPickerDialog,选择器弹框
*/
textPickerDialog(index: number) {
if (index == 0) {
DialogHelper.showTextPickerDialog({
title: "请选择",
range: this.likes,
// range: this.rangeContent,
onAction: (action: number, dialogId: string, value: string | string[]) => {
if (action == DialogAction.SURE) {
ToastUtil.showToast(`已选择:${value as string}`);
}
}
})
} else if (index == 1) {
DialogHelper.showTextPickerDialog({
autoCancel: false,
title: "请选择",
titleFontColor: Color.Pink,
titleBackground: "#E6E6E6",
primaryButton: "取消",
secondaryButton: {
value: "确定",
fontColor: Color.Red,
},
range: this.multis,
value: ["apple2", "orange3", "peach1"], //设置选中
onAction: (action: number, dialogId: string, value: string | string[]) => {
if (action == DialogAction.SURE) {
ToastUtil.showToast(`已选择:${value as string}`);
}
},
onChange: (value: string | string[], index: number | number[]): void => {
LogUtil.error(`value: ${value}`);
LogUtil.error(`index: ${index}`);
}
})
} else if (index == 2) {
let aiHuiList = AreaHelper.getCityByNameSync("安徽省");
DialogHelper.showTextPickerDialog({
range: aiHuiList,
onAction: (action: number, dialogId: string, value: string | string[]) => {
if (action == DialogAction.SURE) {
ToastUtil.showToast(`已选择:${value}`);
}
}
})
} else if (index == 3) {
let areas = AreaHelper.getAreaSync();
DialogHelper.showTextPickerDialog({
title: "请选择收货地址",
range: areas,
onAction: (action: number, dialogId: string, value: string | string[]) => {
if (action == DialogAction.SURE) {
ToastUtil.showToast(`已选择:${value}`);
}
}
})
}
}
/**
* DatePickerDialog,日期选择器弹框
*/
datePickerDialog(index: number) {
if (index == 0) {
DialogHelper.showDatePickerDialog({
dateType: DateType.YmdHms,
onAction: (action: number, dialogId: string, date: Date): void => {
if (action == DialogAction.SURE) {
let dateStr = DateUtil.getFormatDateStr(date, "yyyy-MM-dd HH:mm:ss");
ToastUtil.showToast(`选中日期:${dateStr}`);
LogUtil.error(`选中日期:${dateStr}`);
}
}
})
} else if (index == 1) {
DialogHelper.showDatePickerDialog({
primaryButton: {
value: "取消",
fontColor: Color.Red
},
secondaryButton: "确认",
title: "请选择日期",
dateType: DateType.YmdHm,
// selected: new Date("2999-12-16 19:20:35"),
start:new Date("1976-01-01 00:00:00"),
end: new Date(),
onAction: (action: number, dialogId: string, date: Date): void => {
if (action == DialogAction.SURE) {
let dateStr = DateUtil.getFormatDateStr(date, "yyyy-MM-dd HH:mm");
ToastUtil.showToast(`选中日期:${dateStr}`);
LogUtil.error(`选中日期:${dateStr}`);
}
}
})
} else if (index == 2) {
DialogHelper.showDatePickerDialog({
title: "请选择年月日",
dateType: DateType.YmdH,
canLoop: false,
onAction: (action: number, dialogId: string, date: Date): void => {
if (action == DialogAction.SURE) {
let dateStr = DateUtil.getFormatDateStr(date, "yyyy-MM-dd HH");
ToastUtil.showToast(`选中日期:${dateStr}`);
LogUtil.error(`选中日期:${dateStr}`);
}
}
})
} else if (index == 3) {
DialogHelper.showDatePickerDialog({
title: "请选择时分秒",
transition: AnimationHelper.transitionInDown(300),
dateType: DateType.Hms,
onAction: (action: number, dialogId: string, date: Date): void => {
if (action == DialogAction.SURE) {
let dateStr = DateUtil.getFormatDateStr(date, "HH:mm:ss");
ToastUtil.showToast(`选中日期:${dateStr}`);
LogUtil.error(`选中日期:${dateStr}`);
}
}
})
}
}
/**
* 显示自定义弹窗
*/
customDialog(index: number) {
if (index == 0) {
//自定义一个Tip弹出框
let tipOptions: TipOptions = {
autoCancel: true,
backCancel: true,
title: "提交成功",
content: "请假单已成功提交给您的领导啦",
levelMode:LevelMode.EMBEDDED,
onAction: (action, dialogId) => {
router.pushUrl({ url: "pages/spinkit/SpinKit1" });
// DialogHelper.closeDialog(dialogId);
}
};
DialogHelper.showCustomDialog(wrapBuilder(TipBuilder), tipOptions);
} else if (index == 1) {
//自定义一个底部的MenuLayout
let menuOptions: MenuOptions = {
transition:AnimationHelper.transitionInDown(),
alignment: DialogAlignment.Bottom,
title: "分享",
menus: Utils.getMenus(),
onItemClick: (menu) => {
DialogHelper.showToast(menu.text);
DialogHelper.closeDialog(menuOptions.dialogId ?? "");
}
};
DialogHelper.showCustomDialog(wrapBuilder(MenuBuilder), menuOptions);
} else if (index == 2) {
//自定义一个左边的DrawerLayout
let drawer: DrawerOptions = {
width: px2vp(DisplayUtil.getWidth()) - 50, //自定义的宽度
autoCancel: false,
alignment: DialogAlignment.TopStart,
keyboardAvoidMode: KeyboardAvoidMode.NONE,
transition: AnimationHelper.transitionInLeft(250)
};
DialogHelper.showCustomDialog(wrapBuilder(DrawerBuilder), drawer);
} else {
//基于Lottie动画的自定义加载框
let loadOptions: LoadOptions = new LoadOptions();
loadOptions.backCancel = true;
DialogHelper.showCustomDialog(wrapBuilder(LottieBuilder), loadOptions);
setTimeout(() => {
DialogHelper.closeDialog(loadOptions.dialogId ?? "");
}, 10000)
}
}
/**
* 显示自定义半模态
*/
showBindSheet(index: number) {
// let targetId = this.getUniqueId();
let targetId = "10001";
//自定义一个底部的MenuLayout
let menuOptions: MenuSheetOptions = {
dialogId: DialogHelper.generateId(),
title: {
title: "分享我的动态",
},
detents: [SheetSize.FIT_CONTENT, SheetSize.MEDIUM, SheetSize.FIT_CONTENT],
borderWidth: 10,
borderColor: Color.White,
targetId: targetId,
mode: SheetMode.EMBEDDED,
preferType: SheetType.BOTTOM,
menus: Utils.getMenus(),
onItemClick: (menu) => {
router.pushUrl({ url: "pages/spinkit/SpinKit1" });
DialogHelper.showToast(menu.text);
}
};
DialogHelper.showBindSheet(wrapBuilder(MenuSheetBuilder), menuOptions);
}
/**
* 显示进度加载类弹出框
*/
loadingDialog(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showLoadingDialog({ autoCancel: true })
} else if (index == 1) {
//自定义文字内容
DialogHelper.showLoadingDialog({
loadColor: Color.White,
content: "努力加载中",
fontSize: 15,
backgroundColor: '#ED000000',
autoCancel: true,
})
} else if (index == 2) {
//指定加载动画和加载动画的颜色
DialogHelper.showLoadingDialog({
loadType: SpinType.spinH,
loadColor: $r('app.color.color_main'),
backgroundColor: '#DD000000',
fontColor: Color.White,
borderRadius: 8
})
} else {
//禁止点击返回关闭弹窗 和 禁止点击遮蔽层关闭弹窗
let dialogId = DialogHelper.showLoadingDialog({
loadType: SpinType.spinU,
loadColor: $r('app.color.color_main'),
marginTop: 20,
content: "努力加载中",
fontColor: $r('app.color.color_main'),
autoCancel: false,
backCancel: false
})
//延时关闭
let timeoutID = setTimeout(() => {
DialogHelper.closeDialog(dialogId); //关闭加载框
clearTimeout(timeoutID);
}, 5000);
}
}
/**
* 进度条加载弹框
*/
loadingProgress(index: number) {
if (index == 0) {
//简单使用
this.progress = 50;
let dialogId = DialogHelper.showLoadingProgress({
progress: this.progress,
});
//模拟网络
let intervalID = setInterval(() => {
if (this.progress < 100) {
this.progress = this.progress + 1
DialogHelper.updateLoading(dialogId, "", this.progress)
console.log("progress: " + this.progress);
} else {
DialogHelper.closeDialog(dialogId); //加载成功
ToastUtil.showToast("加载成功啦~");
clearInterval(intervalID);
}
}, 100);
} else if (index == 1) {
//自定义加载内容和主题色
this.progress = 10;
let dialogId = DialogHelper.showLoadingProgress({
progress: this.progress,
content: "努力加载中",
loadColor: $r('app.color.color_main'),
fontColor: $r('app.color.color_main'),
autoCancel: false,
shadow: { radius: 0 }
})
//模拟网络
let intervalID = setInterval(() => {
if (this.progress < 100) {
this.progress = this.progress + 1
DialogHelper.updateLoading(dialogId, undefined, this.progress)
console.log("progress: " + this.progress);
} else {
DialogHelper.closeDialog(dialogId); //加载成功
ToastUtil.showToast("加载成功啦~");
clearInterval(intervalID);
}
}, 150);
} else if (index == 2) {
//自定义加载内容和主题色
this.progress = 0;
let dialogId = DialogHelper.showLoadingProgress({
progress: this.progress,
content: "文件下载中",
loadColor: Color.White,
autoCancel: false,
})
//模拟网络
let intervalID = setInterval(() => {
if (this.progress < 100) {
this.progress = this.progress + 1
DialogHelper.updateLoading(dialogId, undefined, this.progress)
console.log("progress: " + this.progress);
} else {
DialogHelper.closeDialog(dialogId); //加载成功
ToastUtil.showToast("下载成功啦~");
clearInterval(intervalID);
}
}, 200);
}
}
/**
* 自定义的吐司
*/
toast(index: number) {
if (index == 0) {
//简单使用,长文本
DialogHelper.showToast("女人什么年龄是最好?任何年龄都可以。因为每个年龄段都有不一样的美。看XX的穿搭你就会发现成熟女性的魅力,反而会更加的吸引人。难道这就是传说中的可爱在性感面前真的不值得一提吗?")
} else if (index == 1) {
//自定义的长吐司
DialogHelper.showToastLong("这是一个自定义的长吐司呀~~~", {
fontColor: Color.White,
backgroundColor: "#DD0066FF",
bottom: "120vp"
});
} else if (index == 2) {
//自定义吐司
DialogHelper.showToast("这是一个居中的自定义吐司", {
fontColor: Color.Red,
backgroundColor: Color.Black,
alignment: Alignment.Center,
});
} else {
//自定义吐司
DialogHelper.showToastLong("这是一个距离顶部100vp的自定义吐司", {
fontColor: Color.White,
backgroundColor: "#99FF0000",
alignment: Alignment.Top,
offset: { dx: 0, dy: vp2px(100) }
});
}
}
/**
* 带图标的吐司Tip
*/
toastTip(index: number) {
if (index == 0) {
//简单使用
DialogHelper.showToastTip({
message: "操作成功",
imageRes: $r('app.media.icon_tip_ok')
})
} else if (index == 1) {
DialogHelper.showToastTip({
message: "操作失败",
imageRes: $r('app.media.icon_tip_warn')
})
} else if (index == 2) {
//自定义背景和圆角
DialogHelper.showToastTip({
message: "操作成功",
imageRes: $r('app.media.icon_tip_info'),
backgroundColor: $r('app.color.color_main'),
borderRadius: 6
})
} else {
//自定义颜色和时长
DialogHelper.showToastTip({
message: "操作成功",
imageRes: $r('app.media.icon_tip_ok'),
// fontColor: $r('app.color.color_main'),
alignment: DialogAlignment.Bottom,
offset: { dy: -100, dx: 0 },
duration: 2000,
orientation: Orientation.HORIZONTAL,
imageSize: { height: 24, width: 24 },
margin: 8,
})
}
}
build() {
Column() {
TitleView({ title: "弹框指南" })
Scroll(this.scroller) {
Column() {
CardButton({
label: "弹框,加载框、吐司 的统一配置",
buttons:["配置一", "配置二", "默认的配置"],
onClickAction: (index: number): void => {
if (index === 0) {
this.customConfig1();
} else if (index === 1) {
this.customConfig2();
} else {
this.defaultConfig();
}
}
})
CardButton({
label: "AlertDialog",
content: "操作确认类弹出框",
onClickAction: (index: number): void => this.alertDialog(index)
})
CardButton({
label: "ConfirmDialog",
content: "信息确认类弹出框",
onClickAction: (index: number): void => this.confirmDialog(index)
})
CardButton({
label: "TipsDialog",
content: "提示弹出框",
onClickAction: (index: number): void => this.tipsDialog(index)
})
CardButton({
label: "SelectDialog",
content: "选择类弹出框",
num: 3,
onClickAction: (index: number): void => this.selectDialog(index)
})
CardButton({
label: "TextInputDialog",
content: "单行文本输入弹出框",
num: 3,
onClickAction: (index: number): void => this.textInputDialog(index)
})
CardButton({
label: "TextAreaDialog",
content: "多行文本输入弹出框",
num: 3,
onClickAction: (index: number): void => this.textAreaDialog(index)
})
CardButton({
label: "CustomContentDialog",
content: "显示自定义内容区弹出框,同时支持定义操作区按钮样式",
num: 3,
onClickAction: (index: number): void => this.customContentDialog(index)
})
CardButton({
label: "BottomSheetDialog",
content: "显示动作面板",
num: 3,
onClickAction: (index: number): void => this.bottomSheetDialog(index)
})
CardButton({
label: "ActionSheetDialog",
content: "显示动作面板(IOS风格)",
num: 3,
onClickAction: (index: number): void => this.actionSheetDialog(index)
})
CardButton({
label: "TextPickerDialog",
content: "选择器弹框-TextPicker",
num: 4,
onClickAction: (index: number): void => this.textPickerDialog(index)
})
CardButton({
label: "DatePickerDialog",
content: "日期选择器弹框",
num: 4,
onClickAction: (index: number): void => this.datePickerDialog(index)
})
CardButton({
label: "CustomDialog",
content: "自定义弹窗",
onClickAction: (index: number): void => this.customDialog(index)
})
CardButton({
label: "BindSheet",
content: "自定义半模态",
onClickAction: (index: number): void => this.showBindSheet(index)
})
.id("10001")
CardButton({
label: "LoadingDialog",
content: "进度加载类弹出框",
onClickAction: (index: number): void => this.loadingDialog(index)
})
CardButton({
label: "LoadingProgress",
content: "进度条加载弹框",
num: 3,
onClickAction: (index: number): void => this.loadingProgress(index)
})
CardButton({
label: "Toast",
content: "自定义的吐司",
onClickAction: (index: number): void => this.toast(index)
})
CardButton({
label: "ToastTip",
content: "带图标的吐司Tip",
onClickAction: (index: number): void => this.toastTip(index)
})
.margin({ bottom: 15 })
}
}
.layoutWeight(1)
.width('100%')
.backgroundColor($r('app.color.main_background'))
}
}
//自定义文本
@Builder
customTextBuilder(content: string) {
Text(content)
.fontFamily('MyFont')
.fontColor($r('app.color.font_title'))
.fontSize(16)
.padding(5)
.textShadow({
color: Color.Brown,
type: ShadowType.COLOR,
radius: 3,
offsetX: 5,
offsetY: 5,
fill: false
})
}
//自定义文本和图片
@Builder
customTextImgBuilder(content: ResourceStr, src: Resource) {
CustomImageView({ content: content, src: src })
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tongyuyan/harmony-utils.git
git@gitee.com:tongyuyan/harmony-utils.git
tongyuyan
harmony-utils
harmony-utils
master

搜索帮助