#鸿蒙通关秘籍#如何创建和使用自定义对话框组件 TextDialog?

HarmonyOS
2024-11-27 10:50:03
904浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
网络小魔头

创建自定义对话框组件 TextDialog 需要实现以下步骤:

  • 定义一个自定义对话框组件,包含一个显示文字内容的 Text 元素。
import {CustomDialogController } from '@ohos.ets';

@CustomDialog
struct TextDialog {
  @Link textValue: string;
  controller: CustomDialogController;

  build() {
    Column() {
      Text(`${this.textValue}`)
        .fontSize(20)
    }.borderRadius(10).padding({ top: 25 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 在主组件中,实例化 CustomDialogController,设置显示内容并在点击事件中调用 open 方法展示对话框。
import { CustomDialogController } from '@ohos.ets';
import { TextDialog } from './TextDialog';

@Entry
@Component
export struct ParticipateIn {
  @State textValue: string = '您没有该选项的权限';
  dialogController: CustomDialogController | null = new CustomDialogController({
  builder: CustomDialogExample({
    textValue: $textValue
  })
})

  build() {
    Column() {
      Button('显示提示')
        .onClick(() => {
          this.textValue = '您没有该选项的权限';
          this.dialogController?.open();
        })
        .margin({ top: 25 })
    }.borderRadius(10).padding({ top: 25 })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
分享
微博
QQ
微信
回复
2024-11-27 11:10:09


相关问题
如何封装一个自定义Dialog对话框
3058浏览 • 1回复 待解决
HarmonyOS 自定义对话框的控制器
591浏览 • 1回复 待解决
HarmonyOS class中创建对话框不能显示
807浏览 • 1回复 待解决
HarmonyOS 对话框布局
597浏览 • 1回复 待解决
HarmonyOS 弹出对话框
917浏览 • 1回复 待解决