HarmonyOS ArkTS怎么写回调事件监听

HarmonyOS
2024-12-25 12:13:31
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

参考示例如下:

export interface test {
  getTest: (info: TestInterface) => void
}

export class TestInterface {
  public test: test | null = null
  public infos: string = ""

  public setTestListener(test: test) {
    this.test = test
  }

  public getATest(info: TestInterface) {
    this.test?.getTest(info)
  }
}

export class TestClass {
  public t1: TestInterface | null = null
  constructor() {
    this.t1 = new TestInterface()

    let test: test = {
      getTest: (info: TestInterface) => {
        console.log("this interface has been constructed, and this method has done, the info is : " + info.infos)
      }
    }
    this.t1.setTestListener(test)
  }

  doATest(info: string) {
    this.t1!.infos = info
    this.t1?.getATest(this.t1)
  }
}

@Entry(localStorage)
@Component
struct Index {
  tc: TestClass = new TestClass()

  build() {
    Column() {
      Text("test")
      Button("click").onClick(() => {
        this.tc.doATest("叮咚FM")
      })
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
分享
微博
QQ
微信
回复
2024-12-25 14:08:36
相关问题
HarmonyOS 如何编写回函数?
509浏览 • 1回复 待解决
HarmonyOS 如何写回函数
422浏览 • 1回复 待解决
鸿蒙 怎么监听开机事件
4242浏览 • 1回复 待解决
HarmonyOS 属性动画怎么监听帧回
1035浏览 • 1回复 待解决
HarmonyOS 控件的长按事件怎么监听
917浏览 • 1回复 待解决
HarmonyOS 事件
1025浏览 • 1回复 待解决
怎么监听键盘的弹起和关闭事件
3525浏览 • 1回复 待解决
HarmonyOS 网络监听多次
840浏览 • 1回复 待解决
HarmonyOS 点击事件监听问题
810浏览 • 1回复 待解决
HarmonyOS 键盘事件监听问题
1500浏览 • 1回复 待解决
HarmonyOS 如何监听屏幕旋转事件
633浏览 • 1回复 待解决
HarmonyOS connection监听断网事件
949浏览 • 1回复 待解决
HarmonyOS 点击事件的回方法
1028浏览 • 1回复 待解决