当前位置:网站首页>Kotlin implements a simple login page
Kotlin implements a simple login page
2022-06-25 10:22:00 【Peach doesn't come out】
technological process : Enter login nickname + password , Click the login button , Login success displays success toast And enter Details page , Login failure displays failure toast
Kotlin Realize simple login page video
Source code :
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.
* The login page
*
* @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"
}
// Initial name & Password noncompliance
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) {
// Get the current input
val input = s.toString()
// Validation rules
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) {
// Get the current input
val input = s.toString()
// Validation rules
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 All legal
Toast.makeText(this, "Login success!", Toast.LENGTH_SHORT).show()
// Jump to login success page
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.
* Login success page
*
* @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>边栏推荐
- Shuttle JSON, list, map inter transfer
- String longest common prefix
- 什么是 CRA
- Flask博客实战 - 实现个人中心及权限管理
- I'm afraid of the goose factory!
- The path of Architects
- Etcd tutorial - Chapter 4 etcd cluster security configuration
- Basic use and principle of Minio
- ‘Flutter/Flutter. h‘ file not found
- This is enough for request & response
猜你喜欢

Bitmap is converted into drawable and displayed on the screen

How to apply for a widget on wechat how to get a widget on wechat

i++ 和 ++i的真正区别

Flutter dialog: cupertinoalertdialog

Yolov5更换上采样方式

How to develop wechat applet? How to open a wechat store

Unreal Engine graphics and text notes: use VAT (vertex animation texture) to make Houdini end on Houdini special effect (ue4/ue5)

Redis(二)分布式锁与Redis集群搭建

Exception: gradle task assemblydebug failed with exit code 1

Flask博客实战 - 实现侧边栏最新文章及搜索
随机推荐
Unique Wulin, architecture selection manual (including PDF)
字符串 实现 strStr()
Macro application connector\
Oracle查询自带JDK版本
Flutter Gaode map privacy compliance error
Exception: gradle task assemblydebug failed with exit code 1
Wearable devices may reveal personal privacy
Request&Response有这一篇就够了
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
Computational Thinking and economic thinking
Identityserver4 definition concept
Deep understanding of JVM - JVM memory model
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Flask博客实战 - 实现侧边栏文章归档及标签
How to install SSL certificates in Microsoft Exchange 2010
Linked list delete nodes in the linked list
Free applet making tool, how to make wechat applet
[today in history] June 24: Netease was established; The first consumer electronics exhibition was held; The first webcast in the world
WebApi性能优化
I have summarized the knowledge points of JS [intermediate and advanced] for you