10. 数字逻辑门(AND门)

场景:演示与门逻辑,只有两个输入都为高电平时,输出才为高。
核心逻辑:简单的状态判断与颜色反馈。

@Entry
@Component
struct LogicGateAND {
  @State inputA: boolean = false
  @State inputB: boolean = false

  build() {
    Column({ space: 40 }) {
      Text('AND Gate 与门').fontSize(24)
      
      Row({ space: 50 }) {
        // Input A
        Column() {
          Text('Input A')
          Circle()
            .width(40)
            .height(40)
            .fill(this.inputA ? Color.Green : Color.Gray)
            .onClick(() => this.inputA = !this.inputA)
        }

        // Gate Symbol
        Text('&').fontSize(40).borderWidth(2).padding(10)

        // Output
        Column() {
          Text('Output')
          Circle()
            .width(40)
            .height(40)
            .fill((this.inputA && this.inputB) ? Color.Green : Color.Gray)
        }
      }
      
      // Input B below A
      Column() {
        Text('Input B')
        Circle()
          .width(40)
          .height(40)
          .fill(this.inputB ? Color.Green : Color.Gray)
          .onClick(() => this.inputB = !this.inputB)
      }

      Text(`逻辑: ${this.inputA && this.inputB ? '1 (TRUE)' : '0 (FALSE)'}`)
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐