弹窗是移动应用中常见的一种用户界面元素,常用于显示一些重要的信息、提示用户进行操作或收集用户输入。ArkTS提供了多种内置的弹窗供开发者使用,如消息提示、警告对话框、选择器弹窗、操作列表弹窗,除此之外还支持自定义弹窗,来满足各种不同的需求。
- 消息提示
- 警告对话框
- 操作列表弹窗
- 选择器弹窗
- 自定义弹窗
1、消息提示
Toast(消息提示)组件,常用于显示一些简短的消息或提示,一般会在短暂停留后自动消失。
使用需要通过 @ohos.promptAction
模块中的showToast(),
方法显示 Toast 提示,使用时需要先导入@ohos.promptAction
模块。
showToast(options: { message: string | Resource,duration?: number,bottom?: string | number})
- message:
message
属性用于设置提示信息 - duration:
duration
属性用于设置提示信息停留时长,单位为毫秒,取值范围是[1500,10000] - bottom:
bottom
属性用于设置提示信息到底部的距离
import promptAction from '@ohos.promptAction'
@Entry
@Component
struct textInput {
@State message: string = '';
build() {
Column({ space: 10 }) {
TextInput({ 'placeholder': '请输入账号', 'text': '' })
.onChange((newText) => {
this.message = newText
})
.placeholderFont({ weight: '800' })
.width(200)
.height(50)
.backgroundColor(Color.Gray)
Button('提交')
.fontSize(25)
.onClick(() => {
console.log(this.message.length + '')
if (this.message.length<5) {
promptAction.showToast({
message:'您的账号有误',
duration:1600,
bottom:150
})
}
})
}
.width('100%')
.height("100%")
.justifyContent(FlexAlign.Center)
}
}
2、警告对话框
AlertDialog(警告对话框)用于向用户发出警告或确认操作的提示,确保用户在敏感操作前进行确认。AlertDialog.show()调用。
@Entry
@Component
struct textInput {
build() {
Column({ space: 10 }) {
Row({ space: 10 }) {
Text("一本正经").fontSize(30)
Text("32套").fontSi