HarmonyOS 有回调函数的全局自定义弹窗代码例子

HarmonyOS
2024-12-18 15:52:28
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get
// CustomDialog_Globally.ets
import { BusinessError } from '@ohos.base';
// CustomDialog_Globally_other01.ets
import { ComponentContent } from '@kit.ArkUI';
import { buildText, Params } from './CustomDialog_Globally';

export class Params {
  text: string = ""
  callback: (volumeType: string, value: number) => void = () => {
  };

  constructor(text: string, callback: (volumeType: string, value: number) => void) {
    this.text = text;
    this.callback = callback;
  }
}

@Builder
export function buildText(params: Params) {

  // 弹窗内容
  Column() {
    Text(params.text)
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .margin({ bottom: 36 })
    Button("登录")
      .onClick(() => {
        params.text = 'button 点击登录了'
        // 登录结果回调
        params.callback('1', 2)
      })
  }.backgroundColor(Color.Yellow)
}


@Entry
@Component
struct CustomDialog_Globally {
  @State message: string = "hello"

  build() {
    Row() {
      Column() {
        Button("click me")
          .onClick(() => {
            let uiContext = this.getUIContext();
            let promptAction = uiContext.getPromptAction();
            // let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));
            try {
              // promptAction.openCustomDialog(contentNode);
            } catch (error) {
              // let message = (error as BusinessError).message;
              let code = (error as BusinessError).code;
              // console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
            }
            ;
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}


@Entry
@Component
struct CustomDialog_Globally_other01 {
  @State message: string = 'Hello World';

  build() {
    Column() {
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          let uiContext = this.getUIContext();
          let promptAction = uiContext.getPromptAction();
          let callback = (volumeType: string, value: number) => {
            //处理返回值的逻辑
            console.log(`OpenCustomDialog args error code is ${volumeType}, message is ${value}`);
            this.message = volumeType;
          }
          let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message, callback));
          try {
            promptAction.openCustomDialog(contentNode);
          } catch (error) {
            let message = (error as BusinessError).message;
            let code = (error as BusinessError).code;
            console.error(`OpenCustomDialog args error code is ${code}, message is ${message}`);
          }
          ;
        })
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-18 18:50:44
相关问题
组件自定义函数实现
897浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗实现
445浏览 • 1回复 待解决
HarmonyOS 使用全局自定义弹窗
392浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗demo
754浏览 • 1回复 待解决
HarmonyOS 如何声明全局函数
610浏览 • 1回复 待解决
HarmonyOS 如何创建自定义全局弹窗
304浏览 • 1回复 待解决
HarmonyOS 全局自定义弹窗无法弹出
437浏览 • 1回复 待解决
HarmonyOS 自定义interface问题
554浏览 • 1回复 待解决
如何封装全局自定义弹窗
579浏览 • 1回复 待解决
@Builder自定义构建函数,如何参?
695浏览 • 1回复 待解决
HarmonyOS 全局自定义字体
422浏览 • 1回复 待解决
HarmonyOS 自定义全局dialog
278浏览 • 1回复 待解决
HarmonyOS 全局自定义字体
385浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1489浏览 • 1回复 待解决
如何在全局实现一个自定义dialog弹窗
3199浏览 • 1回复 待解决