鸿蒙计算器demo

 
import { ExpressionEvaluator } from '../helper/CalculateHelper'
import { Button1 } from './Button1'
import { Button2 } from './Button2'
@Entry
@Component
struct Index {
  @State cal: string = ''
  @State result: string = '0'
  build() {
    Column({space:10}){
      Blank()

      Column() {
        Text(`${this.result}`).fontColor(Color.White)
          .fontSize(60)
      }.width('100%')
      .alignItems(HorizontalAlign.End)
      .padding({right: 35})

      Column(){
        Text(`${this.cal}`).fontColor(Color.White)
      }.alignItems(HorizontalAlign.End)
      .width('100%').padding({right: 35})
      .height(15)

      Row({space:10}) {
        Button1({num:'AC',
          buttonColor:0xA5A5A5,
          textColor:Color.Black,
          textSize: 30
        }).onClick(() =>{
          this.result = '0'
          this.cal = ''
        })

        Button1({num:'+/-',buttonColor:0xA5A5A5,
          textColor:Color.Black,
          textSize: 30
        })
        Button1({num:'%',buttonColor:0xA5A5A5,
          textColor:Color.Black,
          textSize: 30
        })
        Button1({num:'÷',buttonColor:0xFF9F0B}).onClick(() =>{
          this.cal += '/'
        })
      }

      Row({space:10}) {
        Button1({num:'7'}).onClick(() =>{
          this.cal += '7'
        })
        Button1({num:'8'}).onClick(() =>{
          this.cal += '8'
        })
        Button1({num:'9'}).onClick(() =>{
          this.cal += '9'
        })
        Button1({num:'x',buttonColor:0xFF9F0B}).onClick(() =>{
          this.cal += '*'
        })
      }
      Row({space:10}) {
        Button1({num:'4'}).onClick(() =>{
          this.cal += '4'
        })
        Button1({num:'5'}).onClick(() =>{
          this.cal += '5'
        })
        Button1({num:'6'}).onClick(() =>{
          this.cal += '6'
        })
        Button1({num:'-',buttonColor:0xFF9F0B}).onClick(() =>{
          this.cal += '-'
        })
      }
      Row({space:10}) {
        Button1({num:'1'}).onClick(() =>{
          this.cal += '1'
        })
        Button1({num:'2'}).onClick(() =>{
          this.cal += '2'
        })
        Button1({num:'3'}).onClick(() =>{
          this.cal += '3'
        })
        Button1({num:'+',buttonColor:0xFF9F0B}).onClick(() =>{
          this.cal += '+'
        })
      }

      Row({space:10}){
        Button2({num:'0'}).onClick(() =>{
          this.cal += '0'
        })
        Button1({num:'.'}).onClick(() =>{
          this.cal += '.'
        })
        Button1({num:'=',
          buttonColor:0xFF9F0B})
          .onClick(() => {
            let calHelper = new ExpressionEvaluator()
            this.result = `${calHelper.evaluate(this.cal)}`
        })
      }.margin({bottom: 50})

    }.width('100%').height('100%')
    .backgroundColor(Color.Black)
  }
}
//button1

@Preview
@Component
export struct Button1{
  num: string = '1'
  buttonColor: number = 0x333333
  textColor: number = Color.White
  textSize: number = 40
  build(){
    Column() {
      Text(`${this.num}`)
        .fontColor(this.textColor)
        .fontSize(this.textSize)
        .fontWeight(FontWeight.Medium)
    }.width(75).height(100)
    .backgroundColor(this.buttonColor)
    .justifyContent(FlexAlign.Center)
    .borderRadius(50)
  }
}


//button2

@Preview
@Component
export struct Button2{
    num: string = '1'
    buttonColor: number = 0x333333
    textColor: number = Color.White
    textSize: number = 40

    build(){
      Column() {
        Text(`${this.num}`)
          .fontColor(this.textColor)
          .fontSize(this.textSize)
          .fontWeight(FontWeight.Medium)
      }.width(160).height(100)
      .backgroundColor(this.buttonColor)
      .justifyContent(FlexAlign.Center)
      .borderRadius(50)
    }

}
以上是大概框架

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值