1# ExceptionPrompt 2 3 4异常提示,适用于有异常需要提示异常内容的情况。 5 6 7> **说明:** 8> 9> 该组件从API Version 11开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 10 11 12## 导入模块 13 14```ts 15import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI' 16``` 17 18 19## 子组件 20 21无 22 23## 属性 24 25不支持[通用属性](ts-universal-attributes-size.md) 26 27## ExceptionPrompt 28 29ExceptionPrompt({ options: PromptOptions, onTipClick?: ()=>void, onActionTextClick?: ()=>void }) 30 31**装饰器类型:**\@Component 32 33**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 34 35**系统能力:** SystemCapability.ArkUI.ArkUI.Full 36 37**参数:** 38 39 40| 名称 | 类型 | 必填 | 装饰器类型 | 说明 | 41| -------- | -------- | -------- | -------- | -------- | 42| options | [PromptOptions](#promptoptions) | 是 | \@Prop | 指定当前异常提示的配置信息。 | 43| onTipClick | ()=>void | 否 | - | 点击左侧提示文本的回调函数。 | 44| onActionTextClick | ()=>void | 否 | - | 点击右侧图标按钮的回调函数。 | 45 46## PromptOptions 47 48PromptOptions定义options的类型。 49 50**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 51 52**系统能力:** SystemCapability.ArkUI.ArkUI.Full 53 54| 名称 | 类型 | 必填 | 说明 | 55| -------- | -------- | -------- | -------- | 56| icon | [ResourceStr](ts-types.md#resourcestr) | 否 | 指定当前异常提示的异常图标式样。 | 57| tip | [ResourceStr](ts-types.md#resourcestr) | 否 | 指定当前异常提示的文字提示内容。<br />支持默认内置四种状态文字资源如下:<br />1.无网络状态:显示网络未连接:引用ohos_network_not_connected。<br />2.网络差状态:显示网络连接不稳定,请点击重试:引用ohos_network_connected_unstable。<br />3.连不上服务器状态:显示无法连接到服务器,请点击重试:引用ohos_unstable_connect_server。<br />4.有网但是获取不到内容状态:显示无法获取位置,请点击重试:引用ohos_custom_network_tips_left。 | 58| marginType | [MarginType](#margintype) | 是 | 指定当前异常提示的边距样式 。 | 59| actionText | [ResourceStr](ts-types.md#resourcestr) | 否 | 指定当前异常提示的右侧图标按钮的文字内容。 | 60| marginTop | [Dimension](ts-types.md#dimension10) | 是 | 指定当前异常提示的距离顶部的位置。 | 61| isShown | boolean | 否 | 指定当前异常提示的显隐状态。<br />true:显示状态。<br />false:隐藏状态。 | 62 63## MarginType 64 65MarginType定义marginType的类型。 66 67**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 68 69**系统能力:** SystemCapability.ArkUI.ArkUI.Full 70 71| 名称 | 值 | 说明 | 72| -------- | -------- | -------- | 73| DEFAULT_MARGIN | 0 | 默认边距:<br />边距1:引用ohos_id_card_margin_start。<br />边距2:引用ohos_id_card_margin_end。 | 74| FIT_MARGIN | 1 | 可适配边距:<br /> 边距1:引用ohos_id_max_padding_start。<br /> 边距2:引用ohos_id_max_padding_end。 | 75 76## 事件 77支持[通用事件](ts-universal-events-click.md) 78 79## 示例 80### 示例1(设置异常提示) 81 82该示例展示了如何设置异常提示的异常图标、异常提示的文字、边距样式和右侧图标按钮的文字内容。 83 84```ts 85import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI' 86 87@Entry 88@Component 89struct Index { 90 @State options: PromptOptions = { 91 icon: $r('sys.media.ohos_ic_public_fail'), 92 tip: '异常提示', 93 marginType: MarginType.DEFAULT_MARGIN, 94 actionText: '设置网络', 95 marginTop: 80, 96 isShown: true, 97 } 98 99 build() { 100 Column() { 101 ExceptionPrompt({ 102 options: this.options, 103 onTipClick: () => { 104 // 单击左侧的文本切换到连接状态 105 }, 106 onActionTextClick: () => { 107 // 点击“设置网络”按钮,打开设置网络弹窗界面 108 }, 109 }) 110 } 111 } 112} 113``` 114 115 116 117### 示例2(设置弹窗类型的异常提示) 118 119该示例使用自定义弹窗设置弹窗类型的异常提示。 120 121```ts 122import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI' 123 124@CustomDialog 125struct CustomDialogExample { 126 @Link textValue: string; 127 @Link inputValue: string; 128 @State options: PromptOptions = { 129 icon: $r('sys.media.ohos_ic_public_fail'), 130 tip: '异常提示!', 131 marginType: MarginType.DEFAULT_MARGIN, 132 actionText: '设置', 133 marginTop: 5, 134 isShown: true, 135 }; 136 cancel: () => void = () => { 137 }; 138 confirm: () => void = () => { 139 }; 140 controller: CustomDialogController; 141 142 // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog, 143 // 那么此处需要将指向自己的controller放在最后 144 build() { 145 Column() { 146 ExceptionPrompt({ 147 options: this.options, 148 }) 149 TextInput({ placeholder: '', text: this.textValue }).margin({ top: 70 }).height(60).width('90%') 150 .onChange((value: string) => { 151 this.textValue = value; 152 }) 153 Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 }) 154 Flex({ justifyContent: FlexAlign.SpaceAround }) { 155 Button('cancel') 156 .onClick(() => { 157 this.controller.close(); 158 this.cancel(); 159 }).backgroundColor(0xffffff).fontColor(Color.Black) 160 Button('confirm') 161 .onClick(() => { 162 this.inputValue = this.textValue; 163 this.controller.close(); 164 this.confirm(); 165 }).backgroundColor(0xffffff).fontColor(Color.Red) 166 }.margin({ bottom: 10 }) 167 } 168 } 169} 170 171@Entry 172@Component 173struct Index1 { 174 @State ButtonText: string = ''; 175 @State MAP_HEIGHT: string = '30%'; 176 @State duration: number = 2500; 177 @State tips: string = ''; 178 @State actionText: string = ''; 179 controller: TextInputController = new TextInputController(); 180 cancel: () => void = () => { 181 }; 182 confirm: () => void = () => { 183 }; 184 @State options: PromptOptions = { 185 icon: $r('sys.media.ohos_ic_public_fail'), 186 tip: '', 187 marginType: MarginType.DEFAULT_MARGIN, 188 actionText: '', 189 marginTop: 80, 190 isShown: true, 191 } 192 @State textValue: string = ''; 193 @State inputValue: string = 'click me'; 194 dialogController: CustomDialogController | undefined = new CustomDialogController({ 195 builder: CustomDialogExample({ 196 cancel: this.onCancel, 197 confirm: this.onAccept, 198 textValue: $textValue, 199 inputValue: $inputValue, 200 }), 201 cancel: this.existApp, 202 autoCancel: true, 203 alignment: DialogAlignment.Bottom, 204 offset: { dx: 0, dy: -20 }, 205 gridCount: 4, 206 customStyle: false, 207 }) 208 209 aboutToDisappear() { 210 this.dialogController = undefined; // 将dialogController置空 211 } 212 213 onCancel() { 214 console.info('Callback when the first button is clicked'); 215 } 216 217 onAccept() { 218 console.info('Callback when the second button is clicked'); 219 } 220 221 existApp() { 222 console.info('Click the callback in the blank area'); 223 } 224 225 build() { 226 Column() { 227 Button('Click Me') 228 .width('30%') 229 .margin({ top: 420 }) 230 .zIndex(999) 231 .onClick(() => { 232 if (this.dialogController != undefined) { 233 this.dialogController.open(); 234 } 235 }) 236 } 237 .height('100%') 238 .width('100%') 239 } 240} 241``` 242 243 244