interface回调如何调用-鸿蒙开发者社区-51CTO.COM

interface回调如何调用

​interface回调如何调用

HarmonyOS
2024-06-03 23:40:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
北风_小浦
@Observed 
export class TestPlanCommonModel { 
  serviceType: string = '' 
  // toggle是否勾选 
  isOn: boolean = true 
  constructor(isOn: boolean, serviceType: string) { 
    this.isOn = isOn 
    this.serviceType = serviceType 
  } 
} 
 
@Observed 
export class TestArray{ 
  testPlanCommonModel: TestPlanCommonModel 
 
  constructor(testPlanCommonModel: TestPlanCommonModel) { 
    this.testPlanCommonModel = testPlanCommonModel 
  } 
} 
 
 
@Entry 
@Component 
struct Test { 
  @State isOnState: boolean = false; 
  @State listA: TestPlanCommonModel[] = [ new TestPlanCommonModel(true, '1'), new TestPlanCommonModel(true, '2'), new TestPlanCommonModel(false, '3'), new TestPlanCommonModel(false, '1'), new TestPlanCommonModel(false, '1')] 
 
  // @State listA: boolean[] = [true, true, true, false, false] 
 
  build() { 
    Column() { 
      Test1({ isOnState: this.isOnState, listA: this.listA }) 
      TestPlanItem({ listA: this.listA }) 
      Button('打印:').onClick(() => { 
        for (let i = 0; i < this.listA.length; i++) { 
          console.log("打印:" + JSON.stringify(this.listA[i])) 
        } 
      }) 
    } 
  } 
} 
 
@Component 
struct Test1 { 
  @Link isOnState: boolean 
  @Link listA: TestPlanCommonModel[] 
 
  build() { 
    Column({ space: 10 }) { 
      Toggle({ type: ToggleType.Switch, isOn: true }) 
        .selectedColor('#007DFF') 
        .switchPointColor('#FFFFFF') 
        .onChange((isOn: boolean) => { 
          console.log("点击总开关-修改listA的所有isOn属性为" + isOn) 
          for (let i = 0; i < this.listA.length; i++) { 
            this.listA[i].isOn = isOn 
          } 
        }) 
      Divider() 
 
    } 
  } 
} 
 
import { MyInterDemo } from '../interface/MyInterDemo' 
 
@Entry 
@Component 
struct TestInterfacePage { 
 
  testInter(inter: MyInterDemo) { 
    console.log('testInter'); 
    inter.func_1(); 
    inter.func_2(); 
    inter.func_3('hello'); 
  } 
 
  build() { 
    Row() { 
      Column() { 
        Button('Test interface') 
          .onClick(() => { 
            let demo: MyInterDemo = { 
              func_1: () => { 
                console.log('func_1'); 
              }, 
              func_2: () => { 
                console.log('func_2'); 
                return true; 
              }, 
              func_3: (arg: string) => { 
                console.log('func_3:' + arg); 
              } 
            } 
            this.testInter(demo); 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
export interface MyInterDemo { 
  func_1: () => void; 
  func_2: () => boolean; 
  func_3: (arg: string) => void; 
}
分享
微博
QQ
微信
回复
2024-06-04 22:35:59
相关问题
lottile动画加载完成调用
927浏览 • 1回复 待解决
HarmonyOS 如何声明全局函数
238浏览 • 1回复 待解决
如何为 C++ 提供函数?
2354浏览 • 1回复 待解决
网络请求后如何进行
944浏览 • 1回复 待解决
Emitter如何声明函数类型
1715浏览 • 1回复 待解决
如何实现拍照预览onPreviewFrame
559浏览 • 1回复 待解决
OpenHarmony idl如何实现异步
4571浏览 • 1回复 待解决
如何实现crash堆栈抓取、crash
1719浏览 • 1回复 待解决
HarmonyOS Web组件
112浏览 • 1回复 待解决
Flutter - EventChannel问题
58浏览 • 1回复 待解决
interface如何间接导出
823浏览 • 1回复 待解决
如何实现短时任务的申请和?
277浏览 • 1回复 待解决
HarmonyOS 活体检测问题
84浏览 • 1回复 待解决
HarmonyOS killAllProcesses()无成功的
47浏览 • 1回复 待解决