Harmony鸿蒙实战开发-个人记事本app「密码登录保护-简介版」【源码在文末】


运行工具:DevEco Studio

一、运行演示

1、登录创建密码

image-20240721145922849

2、首页

image-20240721145938665

3、编辑

image-20240721145951758

4、编辑成功

二、部分代码

import PreferenceUtil from '../../utils/preference/PreferencesLogInfo'
import router from '@ohos.router'
import promptAction from '@ohos.promptAction';

@Entry
@Component

struct Login {
  private preference = PreferenceUtil

  @State password: string = "";
  @State firstLogin:boolean = false;

  // 检查是否首次打开应用
  onPageShow() {
    this.preference.firstLogin().then((f)=>{
      this.firstLogin = f;
    })
  }

  build(){
    Column(){
      Image($r('app.media.icon'))
        .width('144')
        .height('144')
        .margin({ top: 70, bottom:70})
        .align(Alignment.Center)
      if(this.firstLogin){
        Text($r('app.string.createPassword'))
          .fontSize(30)
          .margin({bottom:20})
      }
      else{
        Text($r('app.string.inputPassword'))
          .fontSize(30)
          .margin({bottom:20})
      }

      TextInput({placeholder:$r('app.string.inputPassword')})
        .width('70%')
        .height('5%')
        .type(InputType.Password)
        .onChange((value: string) => {
          this.password = value;
        })

      Button(($r('app.string.loginButton')), { type: ButtonType.Capsule })
        .margin('50')
        .width('30%')
        .onClick(()=>{
          if(this.firstLogin){
            this.createPassword();
          }
          else{
            this.checkLogin();
          }
        })
    }
    .width('100%')
    .height('100%')
    .backgroundColor('#ffe6eaea')
  }
  // 首次打开创建密码
  createPassword(){
    if(this.password != "") {
      this.preference.putPreference('password',`${this.password}`)
      router.replaceUrl({
        url: 'pages/main',
      })
    }
    else{
      promptAction.showToast({
        message: $r('app.string.emptyInputPrompt'),
        duration: 2000
      });
    }
  }

  // 检查密码
  checkLogin(){
    this.preference.checkPassword(this.password)
  }
}

三、源码

相关鸿蒙项目点此专栏

image-20241020235757199

通过百度网盘分享的文件:…zip 链接:百度网盘 请输入提取码

文件已经加密,请点击下方名片获取源码

或:Lvnvn0508

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐