最新鸿蒙HarmonyOS4.0开发登陆的界面2

本文介绍了在Ohos平台上构建一个包含用户名和密码输入以及登录验证功能的组件,展示了如何处理用户输入、状态管理和错误提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

登陆功能

在这里插入图片描述
代码如下:

import router from '@ohos.router';
@Entry
@Component
struct Index {
  @State message: string = 'XXAPP登陆'
  @State userName: string = '';
  @State password: string = '';
  build() {
    Row() {
      Column({space:50}) {
        Image($r('app.media.icon')).width(200).interpolation(ImageInterpolation.High)
        Text(this.message).fontSize(50).fontWeight(FontWeight.Bold)
         TextInput({placeholder:'用户名'}) .onChange((value: string) => {
           this.userName = value;
         })
        TextInput({placeholder:'密码'}).onChange((value: string) => {
          this.password = value;
        })
        Button('确定').onClick(()=>{
          if(isLoginButtonClickable(this.userName,this.password)){
            router.pushUrl({
              url: 'pages/Myprofile'
            })
          }else{

            AlertDialog.show(
              {
                title: '提示',
                message: '用户名或密码错',
                autoCancel: true,
                alignment: DialogAlignment.Bottom,
                offset: { dx: 0, dy: -20 },
                confirm: {
                  value: '确认',
                  action: () => {
                    console.info('Callback when confirm button is clicked');
                  }
                },
                cancel: () => {
                  console.info('Closed callbacks')
                }
              }
            )
          }
        })

      }.justifyContent(FlexAlign.Center).height('100%')
    }
  }
}

/**
 * 根据用户名和密码的输入计算登录按钮是否可点击。
 *
 * @param {string} userName - 用户名,要求为手机的格式。
 * @param {string} password - 密码。
 * @return {boolean} 是否可点击登录按钮。
 */
function isLoginButtonClickable(userName: string, password: string): boolean {
  // 在这里添加检查用户名是否为手机格式的逻辑
  // 例如,可以使用正则表达式验证用户名是否符合手机格式
  const isUserNameValid = /\d{11}/.test(userName)
  return isUserNameValid && password !== ''
}

相关代码可以下载:下载代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老大白菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值