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; 
}
  • 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.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
分享
微博
QQ
微信
回复
2024-06-04 22:35:59
相关问题
HarmonyOS 自定义interface问题
877浏览 • 1回复 待解决
lottile动画加载完成调用
1662浏览 • 1回复 待解决
HarmonyOS 事件
1000浏览 • 1回复 待解决
Emitter如何声明函数类型
2343浏览 • 1回复 待解决
OpenHarmony idl如何实现异步
5996浏览 • 1回复 待解决
如何实现拍照预览onPreviewFrame
1278浏览 • 1回复 待解决
HarmonyOS 如何声明全局函数
874浏览 • 1回复 待解决
HarmonyOS onNewWant未
600浏览 • 1回复 待解决
HarmonyOS Web组件
1156浏览 • 1回复 待解决
Flutter - EventChannel问题
998浏览 • 1回复 待解决
HarmonyOS Watch没有
769浏览 • 1回复 待解决