[Kotlin]手把手教你写一个安卓APP(第一章注册登录)
1.创建项目默认选择Empty Activity
点击Next
2.生成项目设置包名选择开发语言(这里我用的是kotlin)
在生成项目后我们要做的就是添加需要的配置打开我们的app目录下的build.gradle去添加我们所要用到的依赖库这次我们会用到一个储存库MMKV
dependencies {implementation 'com.tencent:mmkv-static:1.0.23'
}
别忘了我们还要在项目层build.gradle中配置阿里云镜像
apply from: "config.gradle"
buildscript {ext.kotlin_version = '1.4.31'repositories {mavenCentral()google()maven { url "https://jitpack.io" }}dependencies {classpath 'com.android.tools.build:gradle:4.0.2'classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files}
}allprojects {repositories {mavenCentral()google()maven { url "https://jitpack.io" }maven { url "https://s01.oss.sonatype.org/content/groups/public" }maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}}/*Partial dependency libraries introduce existing dependencies, which are handled in the following way in order to unify their versions*/configurations.all {resolutionStrategy.eachDependency { details ->def requested = details.requestedif (requested.group == 'org.jetbrains.kotlin') {if (requested.name.startsWith("kotlin-stdlib")) {details.useVersion '1.4.31'}}}}
}task clean(type: Delete) {delete rootProject.buildDir
}
所有配置完成后
3.项目生成后会默认进入到MainActivity中如图
这样我们就可以开始写代码了 因为我们要写一个登录&注册一体的功能 所以这里我重新创建一个LoginActivity用来登录使用 将MainActivity用来登录成功后跳转的指向页面
4.画一个登录注册页面(activity_login.xml)
简单效果图
上图代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/dp_16"android:paddingTop="@dimen/dp_16"android:paddingRight="@dimen/dp_16"android:paddingBottom="@dimen/dp_16"tools:context=".ui.LoginActivity"><EditTextandroid:id="@+id/username"android:layout_width="@dimen/dp_0"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_24"android:layout_marginTop="@dimen/dp_96"android:layout_marginEnd="@dimen/dp_24"android:hint="@string/prompt_email"android:inputType="textEmailAddress"android:selectAllOnFocus="true"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><EditTextandroid:id="@+id/password"android:layout_width="@dimen/dp_0"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_24"android:layout_marginTop="@dimen/dp_8"android:layout_marginEnd="@dimen/dp_24"android:hint="@string/prompt_password"android:imeActionLabel="@string/action_sign_in_short"android:imeOptions="actionDone"android:inputType="textPassword"android:selectAllOnFocus="true"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/username" /><Buttonandroid:id="@+id/login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="start"android:layout_marginStart="@dimen/dp_48"android:layout_marginTop="@dimen/dp_16"android:layout_marginEnd="@dimen/dp_48"android:layout_marginBottom="@dimen/dp_64"android:text="@string/action_sign_in"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/password"app:layout_constraintVertical_bias="0.2" /></androidx.constraintlayout.widget.ConstraintLayout>
5.代码实现登录注册并保存账号密码跳转MainActivity页面
package com.example.htkotlinmvvmdemo1.uiimport android.os.Bundle
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.caspar.base.base.BaseActivity
import com.caspar.base.ext.acStart
import com.caspar.base.utils.MMKVUtil
import com.example.htkotlinmvvmdemo1.config.Constant.LOGINUSERNAME
import com.example.htkotlinmvvmdemo1.databinding.ActivityLoginBinding
import com.example.htkotlinmvvmdemo1.ui.viewmodel.LoginViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch/*** "ht" 创建 2021/12/23* 界面名称以及功能: 登录界面*/
class LoginActivity : BaseActivity<ActivityLoginBinding>() {private val loginViewModel: LoginViewModel by viewModels()override fun initView(savedInstanceState: Bundle?) {//判断账号不为空证明已经注册过了这是点击退出后跳回登录界面 默认将存储的账号显示出来当然也可以不写if (MMKVUtil.decodeString(LOGINUSERNAME).isNotEmpty()) {mBindingView.username.setText(MMKVUtil.decodeString(LOGINUSERNAME))}mBindingView.apply {//这里实现点击登录后的处理mBindingView.login.setOnClickListener {//kotlin的语法很简洁 这里的意思是我们将输入的账号密码进行判断不为空即走下面的跳转方法if (loginViewModel.userEmpty(mBindingView.username.text.toString(),mBindingView.password.text.toString())) {//kotlin协程实现2秒延迟跳转lifecycleScope.launch {showDialog()delay(2000)stopDialog()acStart<MainActivity>()finish()}}}}}override fun initIntent() {}
}
我这里用的是mvvm架构 所以我在贴一下这里的LoginViewModel
package com.example.htkotlinmvvmdemo1.ui.viewmodelimport android.app.Application
import com.caspar.base.base.BaseViewModel
import com.example.htkotlinmvvmdemo1.config.Constant
import com.caspar.base.utils.MMKVUtil/*** "ht" 创建 2021/12/23* 界面名称以及功能: 登录界面*/
class LoginViewModel(application: Application) : BaseViewModel(application) {fun userEmpty(userName: String, userPassword: String): Boolean {//这里的意思是用户名或者密码如果有一个为空就返回false并提示(当然我们也可以添加其他判断比如只能输入数字等等我们这里就不多判断了懂就行)return if (userName.isEmpty() || userPassword.isEmpty()) {toast("账号密码不能为空")false} else {//这里的意思是用户名和密码都不为空就保存下来并返回true后执行延迟跳转(这里我将 MMKV封装了我会贴在下面的)MMKVUtil.encode(Constant.LOGINUSERNAME, userName)MMKVUtil.encode(Constant.LOGINPASSWORD, userPassword)true}}}
这样我们就已经实现了简单的登录注册了 当然我们app一般会有启动页 然后配合启动页去判断有没有注册过 如果注册过我们可以通过MMKV获取储存的账号密码直接登录不需要在跳到登录界面,如果你会问那怎么退出呢 当然我们可以储存那也可以删除 只要在app里添加退出登录 将账号密码清空跳到登录界面重新注册登录,还可以在下次在登录时我们判断账号密码为空了 就跳到登录界面重新注册登录
我里面用到ViewBinding并且封装了BaseActivity大家可以去看我第二个文章直接复制至于协程用法后面我在梳理一下
上面只是很简单的一个登录逻辑 相信大家都看得懂 主要是最基础东西 可能更适合新手吧!
最后看看效果:
1.登录验证失败
2.验证通过延迟跳转
3.登录成功跳转MainActivity
至于最后一个截图里能看到已经做了很多demo里实现了 以下功能:
(1) room数据增删改
(2)模糊搜索
(3)Motionlayout动画布局
(4)相机相册权限申请选择并设置圆角等样式
(5)骨架屏加载
(6)图片中提取选中颜色
(7)RecyclerView多级显示+吸顶
(8)CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout基本使用
(9)Retrofit2+Coroutine(协程)+Flow实现网路请求加数据回调发送等等
(10)实现多语言切换
(10)集成高德地图实现中英文切换 3D地图等显示以及基本配置
(11)一些好用得第三方库如Player音乐播放器,BaseRecyclerViewAdapterHelper万能适配, AgentWeb轻量级而且功能强大的 Web 库
反正是很杂就是想到什么就试着写写玩也方便自己用得时候直接拿来用。
最后想说各位码友可以q我大家多多交流!!!!!!!end~
[Kotlin]手把手教你写一个安卓APP(第一章注册登录)相关推荐
- 手把手教你写一个安卓app
最近有很多小伙伴在后台留言:Android Studio.我想大家是想写一个手机app,前面已经分享了在QT上如何写一个安卓蓝牙app,虽然qt可以做app,但是比起Android Studio还是差 ...
- 手把手教你写一个Matlab App(一)
对于传统工科的学生用的最多的编程软件应该就是matlab,其集成度高,计算能力强,容易上手,颇受大众青睐.今天挖的这个新坑,主要是分享用matlab app designer设计GUI界面的一些方法和 ...
- 手把手教你写一个Matlab App(二)
本章博客主要是介绍matlab app designer的基本功能以及如何制作封面,采用的是Matalb r2018a版本制作. 一.matlab app designer启动和基本功能的介绍 1.如 ...
- 后端思维篇:手把手教你写一个并行调用模板
前言 36个设计接口的锦囊中,也提到一个知识点:就是使用并行调用优化接口.所以接下来呢,就快马加鞭写第二篇:手把手教你写一个并行调用模板~ 一个串行调用的例子(App首页信息查询) Completio ...
- 如何写一个安卓APP
写一个安卓APP需要以下步骤: 首先确定你的APP要实现什么功能,明确需求. 然后准备好开发工具和环境.一般来说,安卓APP的开发工具为Android Studio,需要先下载并安装. 接着,学习Ja ...
- 手把手教你写一个中文聊天机器人
本文来自作者 赵英俊(Enjoy) 在 GitChat 上分享 「手把手教你写一个中文聊天机器人」,「阅读原文」查看交流实录. 「文末高能」 编辑 | 哈比 一.前言 发布这篇 Chat 的初衷是想和 ...
- 手把手教你写一个生成对抗网络
成对抗网络代码全解析, 详细代码解析(TensorFlow, numpy, matplotlib, scipy) 那么,什么是 GANs? 用 Ian Goodfellow 自己的话来说: " ...
- python k线合成_手把手教你写一个Python版的K线合成函数
手把手教你写一个Python版的K线合成函数 在编写.使用策略时,经常会使用一些不常用的K线周期数据.然而交易所.数据源又没有提供这些周期的数据.只能通过使用已有周期的数据进行合成.合成算法已经有一个 ...
- 手把手教你写一个spring IOC容器
本文分享自华为云社区<手把手教你写一个spring IOC容器>,原文作者:技术火炬手. spring框架的基础核心和起点毫无疑问就是IOC,IOC作为spring容器提供的核心技术,成功 ...
最新文章
- File,FileInfo;Directory,DirectoyInfo的区别
- MongoDB学习笔记(一)环境搭建与常用操作
- 引入 Gateway 网关,这些坑一定要学会避开!!!
- 精选CSDN的ACM-ICPC活跃博客
- vrml行走和静止的人代码_CAE二次开发的核心不是代码
- unity5, Configurable Joint: Anchor, Connected Anchor, Auto Configure Connected Anchor
- 一名技术leader的工作随笔
- MATLAB信号处理——信号与系统的分析基础(4)
- QQ文件保险柜与Truecrypt之对比.
- Ford-Fulkerson方法
- Flink二:IngestionTime与ProcessingTime
- Tansat XCO2数据下载
- 计算机视觉、模式识别、机器学习常用牛人主页链接
- Windows系统的Chrome 调试运行在 IOS-safari (iPad/iPhone)上的页面
- Nodejs 开发最佳实践
- 非独立同分布数据孤岛的联邦学习:一项实验研究
- 白银价格转换器(美元/盎司-人民币/克)
- 蓝桥杯十届年号字串 JAVA暴解
- TC SRM 553 DIV2
- 拯救小明(多继承+友元)