ArkUI学习(6)

今天学习的是表单数据收集

这是运行的初始界面,先声明一下,这个代码还存在一些逻辑问题(不影响运行),后续会修改

上图为运行结果,可以看到输入的信息被展示在了下方红色方框,代码如下:

复制代码
@Entry
@Component
struct Index{
  @State username:string=''
  @State userage:number=20
  @State isagree:boolean=false
  @State message:string=''
  @State statu:boolean=true
  build(){
    Column(){
      Text('表单数据收集')
        .fontSize(24)
        .fontWeight(FontWeight.Bold)
        .margin({bottom:30})
      Column({space:15}){
        Text('输入姓名')
          .fontSize(16)
          .fontWeight(FontWeight.Medium)
          .alignSelf(ItemAlign.Start)
        TextInput({placeholder:'请输入姓名'})

          .width('100%')
          .height(50)
          .onChange((value:string)=>{
            this.username=value//将输入的值赋给username
            this.statu=false
          })
      }
      .width('100%')
      .padding(20)
      .backgroundColor('#fffffbfb')
      .borderRadius(10)
      //.margin({bottom:20})
      if(this.statu){
        Text('姓名不能为空')
          .fontSize(16)
          //.margin({bottom:20})
          .alignSelf(ItemAlign.Start)
          .fontColor('#fff50606')
          .margin({bottom:10,left:25})
      }
      Column({space:15}){
        Row(){
          Text('年龄')
            .fontSize(16)
            .fontWeight(FontWeight.Medium)
          Text(`${this.userage}岁`)
            .fontSize(16)
            .fontColor('#ff003fb5')
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
        Slider({//这边有一个需要注意的地方,Slider组件的value属性要求必须是number类型
          value:this.userage,
          min:1,
          max:100,
          step:1
        })
          .width('100%')
          .onChange((value:number)=>{
            this.userage=value
          })
      }
      .width('100%')
      .padding(20)
      .backgroundColor('#ccc')
      .borderRadius(10)
      .margin({bottom:20})
      Column({space:15}){
        Row() {
          Text('同意服务条款')
            .fontSize(16)
            .fontWeight(FontWeight.Medium)
          Toggle({
            type: ToggleType.Switch,
            isOn: this.isagree
          })
            .onChange((isOn: boolean) => {
              this.isagree = isOn
            })
        }
        .width('100%')
        .justifyContent(FlexAlign.SpaceBetween)
      }
      .width('100%')
      .padding(20)
      .backgroundColor('#ccc')
      .borderRadius(10)
      .margin({bottom:30})
      Button('提交表单')
        .width('100%')
        .height(50)
        .backgroundColor(Color.Blue)
        .onClick(()=>{
          if(this.username.trim()===''){

            this.statu=false
            this.message=''
            return
          }
          this.statu=false
          this.message=`提交成功\n姓名:${this.username}\n年龄:${this.userage}岁\n同意条款:${this.isagree}`
        })
      if(this.message!=''){
        Text(this.message)
          .width('100%')
          .fontSize(16)
          .padding(20)
          .margin({top:20})
          .backgroundColor('#ffe90000')
          .borderRadius(10)
      }
    }
    .width('100%')
    .height('100%')
    .padding(20)
  }
}
相关推荐
世人万千丶11 天前
桌面便签小应用 - HarmonyOS ArkUI 开发实战-TextArea与Flex布局-PC版本
华为·harmonyos·鸿蒙·鸿蒙系统
YM52e11 天前
买菜计算器小应用 - HarmonyOS ArkUI 开发实战-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨11 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Animation 动画效果实现-PC版本
学习·华为·harmonyos·鸿蒙
三声三视12 天前
Electron 在鸿蒙 PC 上跑 webview,我是怎么把首屏从 4.2s 干到 1.1s 的
华为·electron·harmonyos·鸿蒙
世人万千丶12 天前
成语接龙小应用 - HarmonyOS ArkUI 开发实战-TextInput与List列表-PC版本
华为·list·harmonyos·鸿蒙·鸿蒙系统
Davina_yu12 天前
自定义弹窗:使用CustomDialogController实现复杂交互(27)
harmonyos·鸿蒙·鸿蒙系统
世人万千丶12 天前
家庭记账本小应用 - HarmonyOS ArkUI 开发实战-Tabs与List组件-PC版本
华为·list·harmonyos·鸿蒙
Davina_yu12 天前
画布Canvas:2D绘图上下文(Context2D)绘制复杂图表(33)
harmonyos·鸿蒙·鸿蒙系统
小雨下雨的雨12 天前
HarmonyOS ArkUI训练营入门-组件掌握系列-Grid 网格布局深度解析-PC版本
学习·华为·harmonyos·鸿蒙·鸿蒙系统
Davina_yu12 天前
定时器与任务调度:setTimeout与setInterval的正确使用(19)
harmonyos·鸿蒙·鸿蒙系统