開發(fā)軟件:Android Studio
1.創(chuàng)建項目默認(rèn)選擇Empty Activity
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?點擊Next
?2.生成項目設(shè)置包名選擇開發(fā)語言(這里我用的是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.requested
if (requested.group == 'org.jetbrains.kotlin') {
if (requested.name.startsWith("kotlin-stdlib")) {
details.useVersion '1.4.31'
}
}
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
所有配置完成后
?3.項目生成后會默認(rèn)進(jìn)入到MainActivity中如圖
這樣我們就可以開始寫代碼了 因為我們要寫一個登錄&注冊一體的功能 所以這里我重新創(chuàng)建一個LoginActivity用來登錄使用 將MainActivity用來登錄成功后跳轉(zhuǎn)的指向頁面
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">
<EditText
android: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" />
<EditText
android: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" />
<Button
android: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.代碼實現(xiàn)登錄注冊并保存賬號密碼跳轉(zhuǎn)MainActivity頁面
package com.example.htkotlinmvvmdemo1.ui
import 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" 創(chuàng)建 2021/12/23
* 界面名稱以及功能: 登錄界面
*/
class LoginActivity : BaseActivity<ActivityLoginBinding>() {
private val loginViewModel: LoginViewModel by viewModels()
override fun initView(savedInstanceState: Bundle?) {
//判斷賬號不為空證明已經(jīng)注冊過了這是點擊退出后跳回登錄界面 默認(rèn)將存儲的賬號顯示出來當(dāng)然也可以不寫
if (MMKVUtil.decodeString(LOGINUSERNAME).isNotEmpty()) {
mBindingView.username.setText(MMKVUtil.decodeString(LOGINUSERNAME))
}
mBindingView.apply {
//這里實現(xiàn)點擊登錄后的處理
mBindingView.login.setOnClickListener {
//kotlin的語法很簡潔 這里的意思是我們將輸入的賬號密碼進(jìn)行判斷不為空即走下面的跳轉(zhuǎn)方法
if (loginViewModel.userEmpty(
mBindingView.username.text.toString(),
mBindingView.password.text.toString()
)
) {
//kotlin協(xié)程實現(xiàn)2秒延遲跳轉(zhuǎn)
lifecycleScope.launch {
showDialog()
delay(2000)
stopDialog()
acStart<MainActivity>()
finish()
}
}
}
}
}
override fun initIntent() {
}
}
?我這里用的是mvvm架構(gòu) 所以我在貼一下這里的LoginViewModel
package com.example.htkotlinmvvmdemo1.ui.viewmodel
import android.app.Application
import com.caspar.base.base.BaseViewModel
import com.example.htkotlinmvvmdemo1.config.Constant
import com.caspar.base.utils.MMKVUtil
/**
* "ht" 創(chuàng)建 2021/12/23
* 界面名稱以及功能: 登錄界面
*/
class LoginViewModel(application: Application) : BaseViewModel(application) {
fun userEmpty(userName: String, userPassword: String): Boolean {
//這里的意思是用戶名或者密碼如果有一個為空就返回false并提示(當(dāng)然我們也可以添加其他判斷比如只能輸入數(shù)字等等我們這里就不多判斷了懂就行)
return if (userName.isEmpty() || userPassword.isEmpty()) {
toast("賬號密碼不能為空")
false
} else {
//這里的意思是用戶名和密碼都不為空就保存下來并返回true后執(zhí)行延遲跳轉(zhuǎn)(這里我將 MMKV封裝了我會貼在下面的)
MMKVUtil.encode(Constant.LOGINUSERNAME, userName)
MMKVUtil.encode(Constant.LOGINPASSWORD, userPassword)
true
}
}
}
這樣我們就已經(jīng)實現(xiàn)了簡單的登錄注冊了 當(dāng)然我們app一般會有啟動頁 然后配合啟動頁去判斷有沒有注冊過 如果注冊過我們可以通過MMKV獲取儲存的賬號密碼直接登錄不需要在跳到登錄界面,如果你會問那怎么退出呢 當(dāng)然我們可以儲存那也可以刪除 只要在app里添加退出登錄 將賬號密碼清空跳到登錄界面重新注冊登錄,還可以在下次在登錄時我們判斷賬號密碼為空了 就跳到登錄界面重新注冊登錄
我里面用到ViewBinding并且封裝了BaseActivity大家可以去看我第二個文章直接復(fù)制至于協(xié)程用法后面我在梳理一下?
上面只是很簡單的一個登錄邏輯 相信大家都看得懂 主要是最基礎(chǔ)東西 可能更適合新手吧!
最后看看效果:
1.登錄驗證失敗
?
2.驗證通過延遲跳轉(zhuǎn)
?3.登錄成功跳轉(zhuǎn)MainActivity
至于最后一個截圖里能看到已經(jīng)做了很多demo里實現(xiàn)了 以下功能:
(1) room數(shù)據(jù)增刪改
(2)模糊搜索
(3)Motionlayout動畫布局
(4)相機(jī)相冊權(quán)限申請選擇并設(shè)置圓角等樣式
(5)骨架屏加載
(6)圖片中提取選中顏色
(7)RecyclerView多級顯示+吸頂
(8)CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout基本使用
(9)Retrofit2+Coroutine(協(xié)程)+Flow實現(xiàn)網(wǎng)路請求加數(shù)據(jù)回調(diào)發(fā)送等等
(10)實現(xiàn)多語言切換
(10)集成高德地圖實現(xiàn)中英文切換 3D地圖等顯示以及基本配置
(11)一些好用得第三方庫如Player音樂播放器,BaseRecyclerViewAdapterHelper萬能適配, AgentWeb輕量級而且功能強大的 Web 庫
反正是很雜就是想到什么就試著寫寫玩也方便自己用得時候直接拿來用。文章來源:http://www.zghlxwxcb.cn/news/detail-422932.html
最后想說各位碼友可以q我大家多多交流?。。。。。。nd~文章來源地址http://www.zghlxwxcb.cn/news/detail-422932.html
到了這里,關(guān)于[Kotlin]手把手教你寫一個安卓APP(第一章注冊登錄)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!