目录
一、流程:
输入登录昵称+密码,点击登录按钮,登录成功显示成功toast并进入详情页面,登录失败显示失败toast
Kotlin实现简单的登录页面视频
二、代码实现:
package com.example.kotlintest
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
/**
* Created by tzbc on 2022/2/17.
* 登录页面
*
* @author tzbc
*/
open class MainActivity : AppCompatActivity(), View.OnClickListener {
companion object {
const val TAG = "MainActivity"
const val LEGAL_NAME = "tzbc"
const val LEGAL_PSD = "123456"
const val KEY_LOGIN_NAME = "KEY_LOGIN_NAME"
const val KEY_LOGIN_PSD = "KEY_LOGIN_PSD"
}
//初始名称&密码不合规
private var nameLegal: Boolean = false
private var psdLegal: Boolean = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initEditText()
initClickListener()
}
private fun initEditText() {
etLoginName.isFocusable = true
etLoginName.addTextChangedListener(nameWatcher)
etLoginPsd.addTextChangedListener(psdWatcher)
}
private fun initClickListener() {
etLoginName.setOnClickListener(this)
etLoginPsd.setOnClickListener(this)
btGoLogin.setOnClickListener(this)
}
private val nameWatcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
//获取当前输入内容
val input = s.toString()
//校验规则
Log.v(TAG, "nameWatcher_input=$input")
nameLegal = input == LEGAL_NAME
Log.v(TAG, "nameWatcher_nameLegal=$nameLegal")
}
}
private val psdWatcher: TextWatcher = object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {
//获取当前输入内容
val input = s.toString()
//校验规则
Log.v(TAG, "psdWatcher_input=$input")
psdLegal = input == LEGAL_PSD
Log.v(TAG, "psdWatcher_psdLegal=$psdLegal")
}
}
override fun onClick(v: View?) {
if (v == null) {
return
}
when (v.id) {
R.id.btGoLogin -> {
if (!nameLegal) {
Toast.makeText(this, "name illegal! Please retry.", Toast.LENGTH_SHORT).show()
return
}
if (!psdLegal) {
Toast.makeText(this, "password illegal! Please retry.", Toast.LENGTH_SHORT)
.show()
return
}
//name&psd均合法
Toast.makeText(this, "Login success!", Toast.LENGTH_SHORT).show()
//跳转到登录成功页面
val intent: Intent = Intent(this, LoginSuccessActivity::class.java)
intent.putExtra(KEY_LOGIN_NAME, etLoginName.text.toString())
intent.putExtra(KEY_LOGIN_PSD, etLoginPsd.text.toString())
startActivity(intent)
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:scaleType="centerCrop"
android:src="@drawable/ee" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="22dp"
android:paddingRight="22dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:text="Welcome to join us!"
android:textColor="#000000"
android:textSize="26dp"
android:textStyle="bold" />
<EditText
android:id="@+id/etLoginName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:hint="Please input your name"
android:textSize="18dp" />
<EditText
android:id="@+id/etLoginPsd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:layout_marginRight="30dp"
android:hint="Please input your password"
android:textSize="18dp" />
<Button
android:id="@+id/btGoLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="36dp"
android:layout_marginTop="50dp"
android:layout_marginRight="36dp"
android:background="@drawable/sign_up_on_bg"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:text="Go To Login"
android:textColor="#ffffff"
android:textSize="18dp" />
</LinearLayout>
</FrameLayout>
package com.example.kotlintest
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.kotlintest.MainActivity.Companion.KEY_LOGIN_NAME
import com.example.kotlintest.MainActivity.Companion.KEY_LOGIN_PSD
import kotlinx.android.synthetic.main.activity_login_success.*
/**
* Created by tzbc on 2022/2/17.
* 登录成功页面
*
* @author tzbc
*/
class LoginSuccessActivity : AppCompatActivity() {
private var successName: String? = null
private var successPsd: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login_success)
successName = intent.getStringExtra(KEY_LOGIN_NAME)
successPsd = intent.getStringExtra(KEY_LOGIN_PSD)
initLoginSuccessInfo()
}
private fun initLoginSuccessInfo() {
tvLoginSuccessName?.text = successName ?: "Empty Name"
tvLoginSuccessPsd?.text = successPsd ?: "Empty Psd"
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:scaleType="centerCrop"
android:src="@drawable/ee" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="22dp"
android:paddingRight="22dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="66dp"
android:text="Welcome to join us!"
android:textColor="#000000"
android:textSize="26dp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvLoginSuccessName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="@+id/tvLoginSuccessPsd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:textSize="20dp" />
</LinearLayout>
</FrameLayout>