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
2122
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```ts
83import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI'
84
85@Entry
86@Component
87struct Index {
88  @State options: PromptOptions = {
89    icon: $r('sys.media.ohos_ic_public_fail'),
90    tip: '异常提示',
91    marginType: MarginType.DEFAULT_MARGIN,
92    actionText: '设置网络',
93    marginTop: 80,
94    isShown:true
95  }
96
97  build() {
98    Column() {
99      ExceptionPrompt({
100        options: this.options,
101        onTipClick: () => {
102            // Click the text on the left to change into the connecting state
103        },
104        onActionTextClick: () => {
105            // Click Set Network to open the Set network pop-up interface
106        },
107      })
108    }
109  }
110}
111```
112
113![ExceptionPrompt1](figures/ExceptionPrompt1.png)
114
115### 示例2
116
117```ts
118import { ExceptionPrompt, PromptOptions, MarginType } from '@kit.ArkUI'
119
120@CustomDialog
121struct CustomDialogExample {
122  @Link textValue: string
123  @Link inputValue: string
124  @State options: PromptOptions = {
125    icon: $r('app.media.ic_public_fail'),
126    tip: '异常提示!',
127    marginType: MarginType.DEFAULT_MARGIN,
128    actionText: '设置',
129    marginTop: 5,
130    isShown: true
131  }
132  cancel: () => void = () => {}
133  confirm: () => void = () => {}
134  controller: CustomDialogController
135  // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在最后
136  build() {
137    Column() {
138      ExceptionPrompt({
139        options: this.options,
140      })
141      TextInput({ placeholder: '', text: this.textValue }).margin({top:70}).height(60).width('90%')
142        .onChange((value: string) => {
143          this.textValue = value
144        })
145      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
146      Flex({ justifyContent: FlexAlign.SpaceAround }) {
147        Button('cancel')
148          .onClick(() => {
149            this.controller.close()
150            this.cancel()
151          }).backgroundColor(0xffffff).fontColor(Color.Black)
152        Button('confirm')
153          .onClick(() => {
154            this.inputValue = this.textValue
155            this.controller.close()
156            this.confirm()
157          }).backgroundColor(0xffffff).fontColor(Color.Red)
158      }.margin({ bottom: 10 })
159    }
160  }
161}
162@Entry
163@Component
164struct Index1 {
165  @State ButtonText: string = ''
166  @State MAP_HEIGHT: string = '30%'
167  @State duration: number = 2500
168  @State tips: string = ''
169  @State actionText: string = ''
170  controller: TextInputController = new TextInputController()
171  cancel: () => void = () => {}
172  confirm: () => void = () => {}
173  @State options: PromptOptions = {
174    icon: $r('app.media.ic_public_fail'),
175    tip: '',
176    marginType: MarginType.DEFAULT_MARGIN,
177    actionText: '',
178    marginTop: 80,
179    isShown: true
180  }
181  @State textValue: string = ''
182  @State inputValue: string = 'click me'
183  dialogController: CustomDialogController | undefined = new CustomDialogController({
184    builder: CustomDialogExample({
185      cancel: this.onCancel,
186      confirm: this.onAccept,
187      textValue: $textValue,
188      inputValue: $inputValue
189    }),
190    cancel: this.existApp,
191    autoCancel: true,
192    alignment: DialogAlignment.Bottom,
193    offset: { dx: 0, dy: -20 },
194    gridCount: 4,
195    customStyle: false
196  })
197
198  aboutToDisappear() {
199    this.dialogController = undefined // 将dialogController置空
200  }
201
202  onCancel() {
203    console.info('Callback when the first button is clicked')
204  }
205
206  onAccept() {
207    console.info('Callback when the second button is clicked')
208  }
209
210  existApp() {
211    console.info('Click the callback in the blank area')
212  }
213
214  build() {
215    Column() {
216      Button('Click Me')
217        .width('30%')
218        .margin({top:420})
219        .zIndex(999)
220        .onClick(()=>{
221          if (this.dialogController != undefined) {
222            this.dialogController.open()
223          }
224        })
225    }
226    .height('100%')
227    .width('100%')
228
229  }
230}
231```
232
233![ExceptionPrompt2](figures/ExceptionPrompt2.gif)
234