正所谓万丈高楼平地起,辉煌只能靠自己!!
我们应用开发也理应如此,所有的应用一开始都必须要实现的两个功能。
没错!你猜的很对,就是我们梦开始的地方,是用户的登录与注册。
登录与注册可谓是每个app最重要的功能,用户信息贯穿了整个app的生命周期中,可见其地位的轻重。
应用场景
用户的注册可以帮助我们更好的对用户信息进行保存分类,继而对用户进行进入app后的后续服务。
用户的登录可以对用户身份信息进行校验,我们可以用正确的信息登录到各种已经注册过的系统中,进行想要的服务和操作=
实现效果
今天我们来通过用户首选项来实现用户注册到登录的完整流程,跟着我的节奏,开始发车!!!(实现的效果如下)
登录
应用场景
用户的注册可以帮助我们更好的对用户信息进行保存分类,继而对用户进行进入app后的后续服务。
用户的登录可以对用户身份信息进行校验,我们可以用正确的信息登录到各种已经注册过的系统中,进行想要的服务和操作=
实现效果
今天我们来通过用户首选项来实现用户注册到登录的完整流程,跟着我的节奏,开始发车!!!(实现的效果如下)
登录
功能分析
1. 拿到用户注册信息
2. 拿到用户输入信息
3. 输入信息与注册信息进行对比,相同则登录成功,不同则提醒用用户
用户注册信息我们需要保存在本地所以需要用到用户首选项进行实现
首先对用户首选项进行简单的介绍
用户首选项为应用提供Key-Value键值型的数据处理能力,支持应用持久化轻量级数据,并对其修改和查询。当用户希望有一个全局唯一存储的地方,可以采用用户首选项来进行存储。Preferences会将该数据缓存在内存中,当用户读取的时候,能够快速从内存中获取数据
运作机制如下图
我们通过api文档来对用户首选项进行一个简单的封装,更加方便的实现我们想要的效果(代码如下)
封装完成之后我们开始在登录页面进行ui方面的实现,先使用text、textinput、button组件来绘制我们的登录界面,并且添加相应的逻辑,如果用户名称和密码有一个没有输入我们让登录按钮显示不同的状态,要想实现需要先定义几个对应的参数(实现代码如下)
未全部输入显示效果如下
用户输入信息符合条件后显示效果如下
实现完成之后,实现用户首选项中查询数据的方法,我们点击提交按钮的时候要进行怕判断
(代码如下)
如果错误会提示用户
这样我们登录页面的UI和逻辑就实现成功了
注册页面我们要实现ui逻辑的同时,还需要把用户填写的数据添加进用户首选项中,如果用户名称和密码有一个没有输入我们让登录按钮显示不同的状态,要想实现需要先定义几个对应的参数(代码如下)
未输入状态显示如图
输入后如下图所示
登录成功后使用router 跳转页面
到这里,我们的鸿蒙登录注册功能就实现完成了,赶快去试试吧