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

登陆功能

代码如下:

typescript 复制代码
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 !== ''
}

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

相关推荐
To_OC9 小时前
踩了个 TS 的坑之后,我终于把 type 和 interface 掰明白了
前端·react.js·typescript
mCell13 小时前
用 Cordis 从零构建一个 Mini DeepSeek Harness
typescript·agent·deepseek
满栀58514 小时前
状态管理:Redux、Vuex、Pinia 核心区别
前端·javascript·typescript
苏灿烤鱼18 小时前
公司**不可计算,就自己做操作系统
rust·typescript·agent
sunly_19 小时前
TypeScript总结:16、面向对象速查
前端·javascript·typescript
gs8014019 小时前
解构 Cordis:面向“时空可组合性”的 TypeScript 元框架深度剖析
前端·javascript·typescript
sunly_1 天前
TypeScript总结:15、类型速查
前端·javascript·typescript
想要成为糕糕手2 天前
🚀 在浏览器里跑 DeepSeek-R1?WebGPU 端侧推理实战(六)—— 完结篇:消息渲染与流式收尾
react.js·typescript·llm
sunly_2 天前
TypeScript:3、类型声明与类型推断
javascript·ubuntu·typescript
京东云开发者2 天前
【全栈实践】第一个 AI Agent 项目:从零搭建 AI 音频创作助手(进阶篇)
typescript·fastapi·vuex