在Stage模型中,WindowStage/Window可以通过loadContent接口加载页面并创建UI的实例,并将页面内容渲染到关联的窗口中,所以UI实例和窗口是一一关联的。一些全局的UI接口是和具体UI实例的执行上下文相关的,在当前接口调用时,通过追溯调用链跟踪到UI的上下文,来确定具体的UI实例。若在非UI页面中或者一些异步回调中调用这类接口,可能无法跟踪到当前UI的上下文,导致接口执行失败。
@ohos.window在API version 10 新增getUIContext接口,获取UI上下文实例UIContext对象,使用UIContext对象提供的替代方法,可以直接作用在对应的UI实例上。
说明:
本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
示例效果请以真机运行为准,当前IDE预览器不支持。
UICon文本
以下API需先使用ohos.window中的getUIContext()方法获取UIContext实例,再通过此实例调用对应方法。本文中UIContext对象以uiContext表示。
getFont的
getFont(): 字体
获取Font对象。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
字体 | 返回Font实例对象。 |
示例:
uiContext.getFont();
getComponentUtils
getComponentUtils(): 组件实用程序
获取ComponentUtils对象。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
组件实用程序 | 返回ComponentUtils实例对象。 |
示例:
uiContext.getComponentUtils();
getUIInspector
getUIInspector(): UIInspector
获取UIInspector对象。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
UIInspector(UI检查器) | 返回UIInspector实例对象。 |
示例:
uiContext.getUIInspector();
getMediaQuery
getMediaQuery(): MediaQuery
获取MediaQuery对象。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
MediaQuery(媒体查询) | 返回MediaQuery实例对象。 |
示例:
uiContext.getMediaQuery();
get路由器
getRouter(): 路由器
获取Router对象。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
路由器 | 返回Router实例对象。 |
示例:
uiContext.getRouter();
getPromptAction
getPromptAction(): 提示操作
获取PromptAction对象。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
提示操作 | 返回PromptAction实例对象。 |
示例:
uiContext.getPromptAction();
animate设置为
animateTo(value: AnimateParam, event: () => void): void
提供animateTo接口来指定由于闭包代码导致的状态变化插入过渡动效。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
价值 | AnimateParam | 是 | 设置动画效果相关参数。 |
事件 | () => 无效 | 是 | 指定显示动效的闭包函数,在闭包函数中导致的状态变化系统会自动插入过渡动画。 |
示例:
// xxx.ets
@Entry
@Component
struct AnimateToExample {
@State widthSize: number = 250
@State heightSize: number = 100
@State rotateAngle: number = 0
private flag: boolean = true
build() {
Column() {
Button('change size')
.width(this.widthSize)
.height(this.heightSize)
.margin(30)
.onClick(() => {
if (this.flag) {
uiContext.animateTo({
duration: 2000,
curve: Curve.EaseOut,
iterations: 3,
playMode: PlayMode.Normal,
onFinish: () => {
console.info('play end')
}
}, () => {
this.widthSize = 150
this.heightSize = 60
})
} else {
uiContext.animateTo({}, () => {
this.widthSize = 250
this.heightSize = 100
})
}
this.flag = !this.flag
})
Button('change rotate angle')
.margin(50)
.rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
.onClick(() => {
uiContext.animateTo({
duration: 1200,
curve: Curve.Friction,
delay: 500,
iterations: -1, // 设置-1表示动画无限循环
playMode: PlayMode.Alternate,
onFinish: () => {
console.info('play end')
}
}, () => {
this.rotateAngle = 90
})
})
}.width('100%').margin({ top: 5 })
}
}
showAlertDialog
showAlertDialog(选项:AlertDialogParamWithConfirm |AlertDialogParamWithButtons |AlertDialogParamWithOptions):无效
显示警告弹窗组件,可设置文本内容与响应回调。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 警报对话框ParamWithConfirm |AlertDialogParamWithButtons |AlertDialogParamWithOptions | 是 | 定义并显示AlertDialog组件。 |
示例:
uiContext.showAlertDialog(
{
title: 'title',
message: 'text',
autoCancel: true,
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: -20 },
gridCount: 3,
confirm: {
value: 'button',
action: () => {
console.info('Button-clicking callback')
}
},
cancel: () => {
console.info('Closed callbacks')
}
}
)
showActionSheet
showActionSheet(value: ActionSheetOptions): void
定义列表弹窗并弹出。
系统能力:SystemCapability.ArkUI.ArkUI.Full
ActionSheetOptions参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
标题 | 资源 |字符串 | 是 | 弹窗标题。 |
消息 | 资源 |字符串 | 是 | 弹窗内容。 |
自动取消 | 布尔 | 否 | 点击遮障层时,是否关闭弹窗。 默认值:true |
确认 | { 值:ResourceStr, 操作:() => void } | 否 | 确认按钮的文本内容和点击回调。 默认值: value:按钮文本内容。 action: 按钮选中时的回调。 |
取消 | () => 无效 | 否 | 点击遮障层关闭dialog时的回调。 |
对准 | 对话框对齐 | 否 | 弹窗在竖直方向上的对齐方式。 默认值:DialogAlignment.Bottom |
抵消 | { dx:长度, dy:长度 } | 否 | 弹窗相对alignment所在位置的偏移量。{ dx:0,dy :0 } |
床单 | 数组<SheetInfo> | 是 | 设置选项内容,每个选择项支持设置图片、文本和选中的回调。 |
SheetInfo接口说明:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
标题 | 资源链 | 是 | 选项的文本内容。 |
图标 | 资源链 | 否 | 选项的图标,默认无图标显示。 |
行动 | ()=>void | 是 | 选项选中的回调。 |
示例:
uiContext.showActionSheet({
title: 'ActionSheet title',
message: 'message',
autoCancel: true,
confirm: {
value: 'Confirm button',
action: () => {
console.log('Get Alert Dialog handled')
}
},
cancel: () => {
console.log('actionSheet canceled')
},
alignment: DialogAlignment.Bottom,
offset: { dx: 0, dy: -10 },
sheets: [
{
title: 'apples',
action: () => {
console.log('apples')
}
},
{
title: 'bananas',
action: () => {
console.log('bananas')
}
},
{
title: 'pears',
action: () => {
console.log('pears')
}
}
]
})
showDatePicker对话框
showDatePickerDialog(选项:DatePickerDialogOptions):无效
定义日期滑动选择器弹窗并弹出。
系统能力:SystemCapability.ArkUI.ArkUI.Full
DatePickerDialogOptions参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
开始 | 日期 | 否 | 设置选择器的起始日期。 默认值:Date('1970-1-1') |
结束 | 日期 | 否 | 设置选择器的结束日期。 默认值:Date('2100-12-31') |
选择 | 日期 | 否 | 设置当前选中的日期。 默认值:当前系统日期 |
月球 | 布尔 | 否 | 日期是否显示为农历。 默认值:false |
show时间 | 布尔 | 否 | 是否展示时间项。 默认值:false |
使用MilitaryTime | 布尔 | 否 | 展示时间是否为24小时制。 默认值:false |
消失文本样式 | PickerTextStyle | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。 |
文本样式 | PickerTextStyle | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。 |
selectedTextStyle | PickerTextStyle | 否 | 设置选中项的文本颜色、字号、字体粗细。 |
onAccept | (值:DatePickerResult) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。 |
on取消 | () => 无效 | 否 | 点击弹窗中的“取消”按钮时触发该回调。 |
onChange | (值:DatePickerResult) => void | 否 | 滑动弹窗中的滑动选择器使当前选中项改变时触发该回调。 |
示例:
let selectedDate: Date = new Date("2010-1-1")
uiContext.showDatePickerDialog({
start: new Date("2000-1-1"),
end: new Date("2100-12-31"),
selected: selectedDate,
onAccept: (value: DatePickerResult) => {
// 通过Date的setFullYear方法设置按下确定按钮时的日期,这样当弹窗再次弹出时显示选中的是上一次确定的日期
selectedDate.setFullYear(Number(value.year), Number(value.month), Number(value.day))
console.info("DatePickerDialog:onAccept()" + JSON.stringify(value))
},
onCancel: () => {
console.info("DatePickerDialog:onCancel()")
},
onChange: (value: DatePickerResult) => {
console.info("DatePickerDialog:onChange()" + JSON.stringify(value))
}
})
showTimePicker对话框
showTimePickerDialog(选项:TimePickerDialogOptions):无效
定义时间滑动选择器弹窗并弹出。
系统能力:SystemCapability.ArkUI.ArkUI.Full
TimePickerDialogOptions参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选择 | 日期 | 否 | 设置当前选中的时间。 默认值:当前系统时间 |
使用MilitaryTime | 布尔 | 否 | 展示时间是否为24小时制,默认为12小时制。 默认值:false |
消失文本样式 | PickerTextStyle | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。 |
文本样式 | PickerTextStyle | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。 |
selectedTextStyle | PickerTextStyle | 否 | 设置选中项的文本颜色、字号、字体粗细。 |
onAccept | (值:TimePickerResult) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。 |
on取消 | () => 无效 | 否 | 点击弹窗中的“取消”按钮时触发该回调。 |
onChange | (值:TimePickerResult) => void | 否 | 滑动弹窗中的选择器使当前选中时间改变时触发该回调。 |
示例:
class sethours{
selectTime: Date = new Date('2020-12-25T08:30:00')
hours(h:number,m:number){
this.selectTime.setHours(h,m)
}
}
uiContext.showTimePickerDialog({
selected: this.selectTime,
onAccept: (value: TimePickerResult) => {
// 设置selectTime为按下确定按钮时的时间,这样当弹窗再次弹出时显示选中的为上一次确定的时间
let time = new sethours()
if(value.hour&&value.minute){
time.hours(value.hour, value.minute)
}
console.info("TimePickerDialog:onAccept()" + JSON.stringify(value))
},
onCancel: () => {
console.info("TimePickerDialog:onCancel()")
},
onChange: (value: TimePickerResult) => {
console.info("TimePickerDialog:onChange()" + JSON.stringify(value))
}
})
showTextPicker对话框
showTextPickerDialog(选项:TextPickerDialogOptions):无效
定义文本滑动选择器弹窗并弹出。
系统能力:SystemCapability.ArkUI.ArkUI.Full
TextPickerDialogOptions参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
范围 | 字符串[] |资源|TextPickerRangeContent[] | 是 | 设置文本选择器的选择范围。不可设置为空数组,若设置为空数组,则不弹出弹窗。 |
选择 | 数 | 否 | 设置选中项的索引值。 默认值:0 |
价值 | 字符串 | 否 | 设置选中项的文本内容。当设置了selected参数时,该参数不生效。如果设置的value值不在range范围内,则默认取range第一个元素。 |
defaultPickerItemHeight | 数字 |字符串 | 否 | 设置选择器中选项的高度。 |
消失文本样式 | PickerTextStyle | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。 |
文本样式 | PickerTextStyle | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。 |
selectedTextStyle | PickerTextStyle | 否 | 设置选中项的文本颜色、字号、字体粗细。 |
onAccept | (值:TextPickerResult) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。 |
on取消 | () => 无效 | 否 | 点击弹窗中的“取消”按钮时触发该回调。 |
onChange | (值:TextPickerResult) => void | 否 | 滑动弹窗中的选择器使当前选中项改变时触发该回调。 |
示例:
{ class setvalue{
select: number = 2
set(val:number){
this.select = val
}
}
class setvaluearr{
select: number[] = []
set(val:number[]){
this.select = val
}
}
let fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
uiContext.showTextPickerDialog({
range: this.fruits,
selected: this.select,
onAccept: (value: TextPickerResult) => {
// 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
let setv = new setvalue()
let setvarr = new setvaluearr()
if(value.index){
value.index instanceof Array?setvarr.set(value.index) : setv.set(value.index)
}
console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
},
onCancel: () => {
console.info("TextPickerDialog:onCancel()")
},
onChange: (value: TextPickerResult) => {
console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
}
})
createAnimator
createAnimator(options: AnimatorOptions): AnimatorResult
定义Animator类。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 动画师选项 | 是 | 定义动画选项。 |
返回值:
类型 | 说明 |
---|---|
动画师结果 | Animator结果接口。 |
示例:
import { AnimatorOptions } from '@ohos.animator';
let options:AnimatorOptions = {
duration: 1500,
easing: "friction",
delay: 0,
fill: "forwards",
direction: "normal",
iterations: 3,
begin: 200.0,
end: 400.0
};
uiContext.createAnimator(options);
runScopedTask
runScopedTask(callback: () => void): 无效
在当前UI上下文执行传入的回调函数。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
回调 | () => 无效 | 是 | 回调函数 |
示例:
uiContext.runScopedTask(
() => {
console.log('Succeeded in runScopedTask');
}
);
字体
以下API需先使用UIContext中的getFont()方法获取到Font对象,再通过该对象调用对应方法。
注册字体
registerFont(options: font.FontOptions):无效
在字体管理中注册自定义字体。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 字体。字体选项 | 是 | 注册的自定义字体信息。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
let font:Font = uiContext.getFont();
font.registerFont({
familyName: 'medium',
familySrc: '/font/medium.ttf'
});
getStstemFontList
getSystemFontList():数组<字符串>
获取系统支持的字体名称列表。
系统能力:SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
数组<字符串> | 系统的字体名列表。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
let font:Font|undefined = uiContext.getFont();
if(font){
font.getSystemFontList()
}
getFontByName
getFontByName(fontName: string): 字体。字体信息
根据传入的系统字体名称获取系统字体的相关信息。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
font名称 | 字符串 | 是 | 系统的字体名。 |
返回值:
类型 | 说明 |
---|---|
字体信息 | 字体的详细信息 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
let font:Font|undefined = uiContext.getFont();
if(font){
font.getFontByName('Sans Italic')
}
组件实用程序
以下API需先使用UIContext中的getComponentUtils()方法获取到ComponentUtils对象,再通过该对象调用对应方法。
getRectangleById
getRectangleById(id:字符串):componentUtils.ComponentInfo
获取组件大小、位置、平移缩放旋转及仿射矩阵属性信息。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
同上 | 字符串 | 是 | 组件唯一标识id。 |
返回值:
类型 | 说明 |
---|---|
ComponentInfo | 组件大小、位置、平移缩放旋转及仿射矩阵属性信息。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
let componentUtils:ComponentUtils = uiContext.getComponentUtils();
let modePosition = componentUtils.getRectangleById("onClick");
let localOffsetWidth = modePosition.size.width;
let localOffsetHeight = modePosition.size.height;
UIInspector(UI检查器)
以下API需先使用UIContext中的getUIInspector()方法获取到UIInspector对象,再通过该对象调用对应方法。
createComponentObserver
createComponentObserver(id: string):检查器。组件观察者
注册组件布局和绘制完成回调通知。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
同上 | 字符串 | 是 | 指定组件id。 |
返回值:
类型 | 说明 |
---|---|
组件观察者 | 组件回调事件监听句柄,用于注册和取消注册监听回调。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
let inspector:UIInspector = uiContext.getUIInspector();
let listener = inspector.createComponentObserver('COMPONENT_ID');
MediaQuery(媒体查询)
以下API需先使用UIContext中的getMediaQuery()方法获取到MediaQuery对象,再通过该对象调用对应方法。
匹配媒体同步
matchMediaSync(条件:字符串): mediaQuery.MediaQueryListener
设置媒体查询的查询条件,并返回对应的监听句柄。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
条件 | 字符串 | 是 | 媒体事件的匹配条件,具体可参考媒体查询语法规则。 |
返回值:
类型 | 说明 |
---|---|
MediaQueryListener | 媒体事件监听句柄,用于注册和去注册监听回调。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
let mediaquery: MediaQuery = uiContext.getMediaQuery();
let listener = mediaquery.matchMediaSync('(orientation: landscape)'); //监听横屏事件
路由器
以下API需先使用UIContext中的getRouter()方法获取到Router对象,再通过该对象调用对应方法。
pushUrl
pushUrl(options: router.RouterOptions):承诺<无效>
跳转到应用内的指定页面。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 路由器。路由器选项 | 是 | 跳转页面描述信息。 |
返回值:
类型 | 说明 |
---|---|
承诺<无效> | 异常返回结果。 |
错误码:
以下错误码的详细介绍,请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | 如果未找到 UI 执行上下文。 |
100002 | 如果 URI 不存在。 |
100003 | 如果页面推送过多。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
try {
router.pushUrl({
url: 'pages/routerpage2',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
})
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushUrl failed, code is ${code}, message is ${message}`);
}
pushUrl
pushUrl(options: router.RouterOptions,回调:AsyncCallback<void>):void
跳转到应用内的指定页面。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 路由器。路由器选项 | 是 | 跳转页面描述信息。 |
回调 | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍,请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | 如果未找到 UI 执行上下文。 |
100002 | 如果 URI 不存在。 |
100003 | 如果页面推送过多。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
router.pushUrl({
url: 'pages/routerpage2',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
}, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushUrl failed, code is ${code}, message is ${message}`);
return;
}
console.info('pushUrl success');
})
pushUrl
pushUrl(options: router.RouterOptions,模式:路由器。RouterMode):Promise<void>
跳转到应用内的指定页面。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 路由器。路由器选项 | 是 | 跳转页面描述信息。 |
模式 | 路由器。路由器模式 | 是 | 跳转页面使用的模式。 |
返回值:
类型 | 说明 |
---|---|
承诺<无效> | 异常返回结果。 |
错误码:
以下错误码的详细介绍,请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | 如果未找到 UI 执行上下文。 |
100002 | 如果 URI 不存在。 |
100003 | 如果页面推送过多。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
try {
routerF.pushUrl({
url: 'pages/routerpage2',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
}, rtm.Standard)
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushUrl failed, code is ${code}, message is ${message}`);
}
pushUrl
pushUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
跳转到应用内的指定页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.RouterOptions | 是 | 跳转页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
100002 | if the uri is not exist. |
100003 | if the pages are pushed too much. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
routerF.pushUrl({
url: 'pages/routerpage2',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
}, rtm.Standard, (err) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushUrl failed, code is ${code}, message is ${message}`);
return;
}
console.info('pushUrl success');
})
替换 Url
replaceUrl(选项:路由器。RouterOptions):承诺<无效>
用应用内的某个页面替换当前页面,并销毁被替换的页面。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 路由器。路由器选项 | 是 | 替换页面描述信息。 |
返回值:
类型 | 说明 |
---|---|
承诺<无效> | 异常返回结果。 |
错误码:
以下错误码的详细介绍,请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | 如果未找到UI执行上下文,则仅投入标准系统。 |
200002 | 如果 URI 不存在。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
try {
router.replaceUrl({
url: 'pages/detail',
params: {
data1: 'message'
}
})
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
}
替换 Url
replaceUrl(选项:路由器。RouterOptions,回调:AsyncCallback<void>):void
用应用内的某个页面替换当前页面,并销毁被替换的页面。
系统能力:SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
选项 | 路由器。路由器选项 | 是 | 替换页面描述信息。 |
回调 | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍,请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | 如果未找到UI执行上下文,则仅投入标准系统。 |
200002 | 如果 URI 不存在。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
router.replaceUrl({
url: 'pages/detail',
params: {
data1: 'message'
}
}, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
return;
}
console.info('replaceUrl success');
})
替换 Url
replaceUrl(options: router.RouterOptions, mode: router.RouterMode): Promise<void>
用应用内的某个页面替换当前页面,并销毁被替换的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.RouterOptions | 是 | 替换页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if can not get the delegate, only throw in standard system. |
200002 | if the uri is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
try {
routerF.replaceUrl({
url: 'pages/detail',
params: {
data1: 'message'
}
}, rtm.Standard)
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
}
replaceUrl
replaceUrl(options: router.RouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
用应用内的某个页面替换当前页面,并销毁被替换的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.RouterOptions | 是 | 替换页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
200002 | if the uri is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
routerF.replaceUrl({
url: 'pages/detail',
params: {
data1: 'message'
}
}, rtm.Standard, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceUrl failed, code is ${code}, message is ${message}`);
return;
}
console.info('replaceUrl success');
});
pushNamedRoute
pushNamedRoute(options: router.NamedRouterOptions): Promise<void>
跳转到指定的命名路由页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 跳转页面描述信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
100003 | if the pages are pushed too much. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
try {
router.pushNamedRoute({
name: 'myPage',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
})
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
}
pushNamedRoute
pushNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void
跳转到指定的命名路由页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 跳转页面描述信息。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
100003 | if the pages are pushed too much. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
router.pushNamedRoute({
name: 'myPage',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
}, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
return;
}
console.info('pushNamedRoute success');
})
pushNamedRoute
pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void>
跳转到指定的命名路由页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 跳转页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
100003 | if the pages are pushed too much. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
try {
routerF.pushNamedRoute({
name: 'myPage',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
}, rtm.Standard)
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
}
pushNamedRoute
pushNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
跳转到指定的命名路由页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 跳转页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
100003 | if the pages are pushed too much. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
routerF.pushNamedRoute({
name: 'myPage',
params: {
data1: 'message',
data2: {
data3: [123, 456, 789]
}
}
}, rtm.Standard, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`pushNamedRoute failed, code is ${code}, message is ${message}`);
return;
}
console.info('pushNamedRoute success');
})
replaceNamedRoute
replaceNamedRoute(options: router.NamedRouterOptions): Promise<void>
用指定的命名路由页面替换当前页面,并销毁被替换的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 替换页面描述信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
try {
router.replaceNamedRoute({
name: 'myPage',
params: {
data1: 'message'
}
})
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
}
replaceNamedRoute
replaceNamedRoute(options: router.NamedRouterOptions, callback: AsyncCallback<void>): void
用指定的命名路由页面替换当前页面,并销毁被替换的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 替换页面描述信息。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router:Router = uiContext.getRouter();
router.replaceNamedRoute({
name: 'myPage',
params: {
data1: 'message'
}
}, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
return;
}
console.info('replaceNamedRoute success');
})
replaceNamedRoute
replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode): Promise<void>
用指定的命名路由页面替换当前页面,并销毁被替换的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 替换页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
返回值:
类型 | 说明 |
---|---|
Promise<void> | 异常返回结果。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if can not get the delegate, only throw in standard system. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
try {
routerF.replaceNamedRoute({
name: 'myPage',
params: {
data1: 'message'
}
}, rtm.Standard)
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
}
replaceNamedRoute
replaceNamedRoute(options: router.NamedRouterOptions, mode: router.RouterMode, callback: AsyncCallback<void>): void
用指定的命名路由页面替换当前页面,并销毁被替换的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.NamedRouterOptions | 是 | 替换页面描述信息。 |
mode | router.RouterMode | 是 | 跳转页面使用的模式。 |
callback | AsyncCallback<void> | 是 | 异常响应回调。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found, only throw in standard system. |
100004 | if the named route is not exist. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
import router from '@ohos.router';
let routerF:Router = uiContext.getRouter();
class routerTmp{
Standard:router.RouterMode = router.RouterMode.Standard
}
let rtm:routerTmp = new routerTmp()
routerF.replaceNamedRoute({
name: 'myPage',
params: {
data1: 'message'
}
}, rtm.Standard, (err: Error) => {
if (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`replaceNamedRoute failed, code is ${code}, message is ${message}`);
return;
}
console.info('replaceNamedRoute success');
});
back
back(options?: router.RouterOptions ): void
返回上一页面或指定的页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.RouterOptions | 否 | 返回页面描述信息,其中参数url指路由跳转时会返回到指定url的界面,如果页面栈上没有url页面,则不响应该情况。如果url未设置,则返回上一页,页面不会重新构建,页面栈里面的page不会回收,出栈后会被回收。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
router.back({url:'pages/detail'});
clear
clear(): void
清空页面栈中的所有历史页面,仅保留当前页面作为栈顶页面。
系统能力: SystemCapability.ArkUI.ArkUI.Full
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
router.clear();
getLength
getLength(): string
获取当前在页面栈内的页面数量。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
string | 页面数量,页面栈支持最大数值是32。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
let size = router.getLength();
console.log('pages stack size = ' + size);
getState
getState(): router.RouterState
获取当前页面的状态信息。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
RouterState | 页面状态信息。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
let page = router.getState();
console.log('current index = ' + page.index);
console.log('current name = ' + page.name);
console.log('current path = ' + page.path);
showAlertBeforeBackPage
showAlertBeforeBackPage(options: router.EnableAlertOptions): void
开启页面返回询问对话框。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | router.EnableAlertOptions | 是 | 文本弹窗信息描述。 |
错误码:
以下错误码的详细介绍请参见ohos.router(页面路由)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
try {
router.showAlertBeforeBackPage({
message: 'Message Info'
});
} catch(error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showAlertBeforeBackPage failed, code is ${code}, message is ${message}`);
}
hideAlertBeforeBackPage
hideAlertBeforeBackPage(): void
禁用页面返回询问对话框。
系统能力: SystemCapability.ArkUI.ArkUI.Full
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
router.hideAlertBeforeBackPage();
getParams
getParams(): Object
获取发起跳转的页面往当前页传入的参数。
系统能力: SystemCapability.ArkUI.ArkUI.Full
返回值:
类型 | 说明 |
---|---|
object | 发起跳转的页面往当前页传入的参数。 |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let router: Router = uiContext.getRouter();
router.getParams();
PromptAction
以下API需先使用UIContext中的getPromptAction()方法获取到PromptAction对象,再通过该对象调用对应方法。
showToast
showToast(options: promptAction.ShowToastOptions): void
创建并显示文本提示框。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | promptAction.ShowToastOptions | 是 | 文本弹窗选项。 |
错误码:
以下错误码的详细介绍请参见ohos.promptAction(弹窗)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let promptAction: PromptAction = uiContext.getPromptAction();
try {
promptAction.showToast({
message: 'Message Info',
duration: 2000
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showToast args error code is ${code}, message is ${message}`);
};
showDialog
showDialog(options: promptAction.ShowDialogOptions, callback: AsyncCallback<promptAction.ShowDialogSuccessResponse>): void
创建并显示对话框,对话框响应结果异步返回。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | promptAction.ShowDialogOptions | 是 | 页面显示对话框信息描述。 |
callback | AsyncCallback<promptAction.ShowDialogSuccessResponse> | 是 | 对话框响应结果回调。 |
错误码:
以下错误码的详细介绍请参见ohos.promptAction(弹窗)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
class buttonsMoabl {
text: string = ""
color: string = ""
}
let promptAction: PromptAction = uiContext.getPromptAction();
try {
promptAction.showDialog({
title: 'showDialog Title Info',
message: 'Message Info',
buttons: [
{
text: 'button1',
color: '#000000'
} as buttonsMoabl,
{
text: 'button2',
color: '#000000'
} as buttonsMoabl
]
}, (err, data) => {
if (err) {
console.info('showDialog err: ' + err);
return;
}
console.info('showDialog success callback, click button: ' + data.index);
});
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showDialog args error code is ${code}, message is ${message}`);
};
showDialog
showDialog(options: promptAction.ShowDialogOptions): Promise<promptAction.ShowDialogSuccessResponse>
创建并显示对话框,对话框响应后同步返回结果。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | promptAction.ShowDialogOptions | 是 | 对话框选项。 |
返回值:
类型 | 说明 |
---|---|
Promise<promptAction.ShowDialogSuccessResponse> | 对话框响应结果。 |
错误码:
以下错误码的详细介绍请参见ohos.promptAction(弹窗)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let promptAction: PromptAction = uiContext.getPromptAction();
try {
promptAction.showDialog({
title: 'Title Info',
message: 'Message Info',
buttons: [
{
text: 'button1',
color: '#000000'
},
{
text: 'button2',
color: '#000000'
}
],
})
.then(data => {
console.info('showDialog success, click button: ' + data.index);
})
.catch((err:Error) => {
console.info('showDialog error: ' + err);
})
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showDialog args error code is ${code}, message is ${message}`);
};
showActionMenu
showActionMenu(options: promptAction.ActionMenuOptions, callback:promptAction.ActionMenuSuccessResponse):void
创建并显示操作菜单,菜单响应结果异步返回。
系统能力: SystemCapability.ArkUI.ArkUI.Full。
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | promptAction.ActionMenuOptions | 是 | 操作菜单选项。 |
callback | promptAction.ActionMenuSuccessResponse | 是 | 菜单响应结果回调。 |
错误码:
以下错误码的详细介绍请参见ohos.promptAction(弹窗)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import promptAction from '@ohos.promptAction';
import { BusinessError } from '@ohos.base';
class buttonsMoabl {
text: string = ""
color: string = ""
}
class dataR{
err:Error = new Error;
data:promptAction.ActionMenuSuccessResponse | undefined = undefined;
}
let dataAMSR:dataR = new dataR()
let promptActionF: PromptAction = uiContext.getPromptAction();
try {
if(dataAMSR.data){
promptActionF.showActionMenu({
title: 'Title Info',
buttons: [
{
text: 'item1',
color: '#666666'
} as buttonsMoabl,
{
text: 'item2',
color: '#000000'
} as buttonsMoabl
]
}, (dataAMSR.data))
if (dataAMSR.err) {
console.info('showActionMenu err: ' + dataAMSR.err);
}else{
console.info('showActionMenu success callback, click button: ' + dataAMSR.data.index);
}
}
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showActionMenu args error code is ${code}, message is ${message}`);
};
showActionMenu
showActionMenu(options: promptAction.ActionMenuOptions): Promise<promptAction.ActionMenuSuccessResponse>
创建并显示操作菜单,菜单响应后同步返回结果。
系统能力: SystemCapability.ArkUI.ArkUI.Full
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
options | promptAction.ActionMenuOptions | 是 | 操作菜单选项。 |
返回值:
类型 | 说明 |
---|---|
Promise<promptAction.ActionMenuSuccessResponse> | 菜单响应结果。 |
错误码:
以下错误码的详细介绍请参见ohos.promptAction(弹窗)错误码。
错误码ID | 错误信息 |
---|---|
100001 | if UI execution context not found. |
示例:
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
import { BusinessError } from '@ohos.base';
let promptAction: PromptAction = uiContext.getPromptAction();
try {
promptAction.showActionMenu({
title: 'showActionMenu Title Info',
buttons: [
{
text: 'item1',
color: '#666666'
},
{
text: 'item2',
color: '#000000'
},
]
})
.then(data => {
console.info('showActionMenu success, click button: ' + data.index);
})
.catch((err:Error) => {
console.info('showActionMenu error: ' + err);
})
} catch (error) {
let message = (error as BusinessError).message;
let code = (error as BusinessError).code;
console.error(`showActionMenu args error code is ${code}, message is ${message}`);
};
最后
有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)资料用来跟着学习是非常有必要的。
这份鸿蒙(HarmonyOS NEXT)资料包含了鸿蒙开发必掌握的核心知识要点,内容包含了(ArkTS、ArkUI开发组件、Stage模型、多端部署、分布式应用开发、音频、视频、WebGL、OpenHarmony多媒体技术、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战等等)鸿蒙(HarmonyOS NEXT)技术知识点。
希望这一份鸿蒙学习资料能够给大家带来帮助,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!
获取这份完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料
鸿蒙(HarmonyOS NEXT)最新学习路线
-
HarmonOS基础技能
- HarmonOS就业必备技能
- HarmonOS多媒体技术
- 鸿蒙NaPi组件进阶
- HarmonOS高级技能
- 初识HarmonOS内核
- 实战就业级设备开发
有了路线图,怎么能没有学习资料呢,小编也准备了一份联合鸿蒙官方发布笔记整理收纳的一套系统性的鸿蒙(OpenHarmony )学习手册(共计1236页)与鸿蒙(OpenHarmony )开发入门教学视频,内容包含:ArkTS、ArkUI、Web开发、应用模型、资源分类…等知识点。
获取以上完整版高清学习路线,请点击→纯血版全套鸿蒙HarmonyOS学习资料
《鸿蒙 (OpenHarmony)开发入门教学视频》
《鸿蒙生态应用开发V2.0白皮书》
《鸿蒙 (OpenHarmony)开发基础到实战手册》
OpenHarmony北向、南向开发环境搭建
《鸿蒙开发基础》
- ArkTS语言
- 安装DevEco Studio
- 运用你的第一个ArkTS应用
- ArkUI声明式UI开发
- .……
《鸿蒙开发进阶》
- Stage模型入门
- 网络管理
- 数据管理
- 电话服务
- 分布式应用开发
- 通知与窗口管理
- 多媒体技术
- 安全技能
- 任务管理
- WebGL
- 国际化开发
- 应用测试
- DFX面向未来设计
- 鸿蒙系统移植和裁剪定制
- ……
《鸿蒙进阶实战》
- ArkTS实践
- UIAbility应用
- 网络案例
- ……
获取以上完整鸿蒙HarmonyOS学习资料,请点击→纯血版全套鸿蒙HarmonyOS学习资料
总结
总的来说,华为鸿蒙不再兼容安卓,对中年程序员来说是一个挑战,也是一个机会。只有积极应对变化,不断学习和提升自己,他们才能在这个变革的时代中立于不败之地。