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)
  }
}
相关推荐
心疼你的一切1 天前
从零到一:鸿蒙健康监测应用的全流程开发实录
人工智能·华为·harmonyos·鸿蒙·鸿蒙系统
梦想不只是梦与想2 天前
鸿蒙与 H5 通信使用的方法及原理
harmonyos·鸿蒙·webview
阿钱真强道2 天前
22 鸿蒙LiteOS 互斥锁(Mutex)实战教程:多任务共享资源保护
harmonyos·鸿蒙·互斥·rk·liteos·瑞芯微·rk2206
坚果派·白晓明3 天前
【鸿蒙PC三方库移植适配框架解读系列】第八篇:扩展lycium框架使其满足rust三方库适配
c语言·开发语言·华为·rust·harmonyos·鸿蒙
坚果派·白晓明3 天前
【鸿蒙PC三方库移植适配框架解读系列】第五篇:完整流程图与角色职责
c语言·c++·华为·harmonyos·鸿蒙
KillerNoBlood3 天前
2026移动端跨平台开发面经总结
android·算法·flutter·ios·移动开发·鸿蒙·kmp
unique_williams3 天前
开源 | 我用 HarmonyOS + Spring Boot 写了一个全栈背单词 App,已上架 GitHub
springboot·鸿蒙
三声三视3 天前
Electron + 鸿蒙分布式投屏:PC 端一键推送画面到鸿蒙设备全实战
分布式·electron·harmonyos·鸿蒙·桌面
UnicornDev3 天前
【Flutter x HarmonyOS 6】魔方计时APP——挑战页面的UI设计
flutter·ui·华为·harmonyos·鸿蒙
三声三视3 天前
鸿蒙 ArkTS 后台任务全攻略:短时任务、长驻任务与延迟任务实战,告别应用被系统杀掉的困境
华为·harmonyos·鸿蒙