当前位置:网站首页>Simple use of Android kotlin
Simple use of Android kotlin
2022-07-22 15:09:00 【It's better to tease a dog than to talk about love】
activity_kotlin.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".Kotlin_Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_60"
android:layout_marginTop="@dimen/dp_60"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login"
android:textColor="@color/login"
android:textSize="@dimen/dp_60" />
</LinearLayout>
<LinearLayout
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_60"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:text="@string/username"
android:textColor="@color/heise"
android:textSize="@dimen/dp_22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="@dimen/dp_16"
android:layout_marginRight="@dimen/dp_16"
android:background="@color/login">
<EditText
android:id="@+id/Edit_UserName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/baise"
android:textColor="@color/heise"
android:textSize="@dimen/dp_18" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_60"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dp_20"
android:text="@string/password"
android:textColor="@color/heise"
android:textSize="@dimen/dp_22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="@dimen/dp_16"
android:layout_marginRight="@dimen/dp_16"
android:background="@color/login">
<EditText
android:id="@+id/Edit_Password"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/baise"
android:textColor="@color/heise"
android:textSize="@dimen/dp_18" />
</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_marginTop="@dimen/dp_60"
android:text="@string/login"
android:textColor="@color/login"
android:textSize="@dimen/dp_22" />
</LinearLayout>
Kotlin_Activity.KT:
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import org.w3c.dom.Text
class Kotlin_Activity : AppCompatActivity() {
private lateinit var Uname: EditText;
private lateinit var Upword: EditText;
private lateinit var Blogin: Button;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kotlin)
Uname = findViewById(R.id.Edit_UserName)
Upword = findViewById(R.id.Edit_Password)
Blogin = findViewById(R.id.btn_login)
init()
}
fun init() {
val sp: SharedPreferences = this.getSharedPreferences("default", Context.MODE_PRIVATE)
val userName = sp.getString("userName", "")
val password = sp.getString("password", "")
if (!TextUtils.isEmpty(userName))
Uname.setText(userName)
if (!TextUtils.isEmpty(password))
Upword.setText(password)
Blogin.setOnClickListener(View.OnClickListener {
login()
})
}
fun login() {
val userName = Uname.getText().toString()
val password = Upword.getText().toString()
val value = when {
TextUtils.isEmpty(userName) -> " Account number cannot be empty !"
TextUtils.isEmpty(password) -> " The password cannot be empty !"
!TextUtils.equals(userName, "542210035") -> " Incorrect account number !"
!TextUtils.equals(password, "123456") -> " Incorrect password !"
else -> " Login successful "
}
Toast.makeText(this, value, Toast.LENGTH_LONG).show()
if (TextUtils.equals(value, " Login successful ")) {
val sp: SharedPreferences = this.getSharedPreferences("default", Context.MODE_PRIVATE)
val editor: SharedPreferences.Editor = sp.edit();
editor.putString("userName", userName)
editor.putString("password", password)
editor.commit()
startActivity(Intent(this, Kotlin_List().javaClass))
}
}
}
activity_kotlin_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Kotlin_List">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/textCZ"
android:layout_width="80dp"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:text=" Vertical display "
android:textColor="@color/baise" />
<TextView
android:id="@+id/textSP"
android:layout_width="80dp"
android:layout_height="50dp"
android:background="@color/colorAccent"
android:gravity="center"
android:text=" Horizontal display "
android:textColor="@color/baise" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/LV"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Kotlin_list.KT:
import android.annotation.SuppressLint
import android.location.Location
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ListView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_kotlin__list.view.*
import youli.com.example.administrator.kotlindemo.Adapters.OnItemClickListener
class Kotlin_List : AppCompatActivity() {
private lateinit var textCZ: TextView
private lateinit var textSP: TextView
private lateinit var lv: RecyclerView
private var textList: ArrayList<String>
private lateinit var adapter: Adapters
private lateinit var mnger: LinearLayoutManager
init {
textList = ArrayList()
textList.add("1")
textList.add("2")
textList.add("3")
textList.add("4")
textList.add("5")
textList.add("6")
textList.add("7")
textList.add("8")
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_kotlin__list)
initView()
}
@SuppressLint("WrongConstant")
fun initView() {
textCZ = findViewById(R.id.textCZ)
textSP = findViewById(R.id.textSP)
lv = findViewById(R.id.LV)
mnger = LinearLayoutManager(this)
mnger.setOrientation(OrientationHelper.VERTICAL) // Vertical display
lv.setLayoutManager(mnger);
adapter = Adapters(textList);
lv.adapter = adapter
adapter.setOnRecyclerViewItemClickListener(obj)
textCZ.setOnClickListener(clinc1)
textSP.setOnClickListener(clinc1)
textCZ.setOnLongClickListener(clinc6)
textSP.setOnLongClickListener(clinc6)
}
/**
* adapter Interface implementation
*/
val obj = object : OnItemClickListener {
override fun onLongClick(position: Int) {
Log.i("TAG", " Long press :${textList.get(position)}")
adapter.setThisPosition(position)
adapter.notifyDataSetChanged()
}
override fun onClick(position: Int) {
Log.i("TAG", " Click on :${textList.get(position)}")
adapter.setThisPosition(position)
adapter.notifyDataSetChanged()
}
}
/**
* In brackets , And use lambda Expression representation
*/
@SuppressLint("WrongConstant")
var clinc1 = View.OnClickListener({ v ->
when (v.id) {
R.id.textCZ -> mnger.setOrientation(OrientationHelper.VERTICAL) // Vertical display
R.id.textSP -> mnger.setOrientation(OrientationHelper.HORIZONTAL) // Horizontal display
}
})
/**
* Delete (), And use lambda Expression representation
*/
var clinc2 = View.OnClickListener { v ->
when (v.id) {
R.id.textCZ -> Log.i("TAG", " vertical ")
R.id.textSP -> Log.i("TAG", " level ")
}
}
// Delete (), Delete lambda expression , Only one parameter can be used it Express
var clinc3 = View.OnClickListener {
when (it.id) {
R.id.textCZ -> Log.i("TAG", " vertical ")
R.id.textSP -> Log.i("TAG", " level ")
}
}
/**
* Long press event
*/
@SuppressLint("WrongConstant")
var clinc6 = View.OnLongClickListener() [email protected]{
when (it.id) {
R.id.textCZ -> mnger.setOrientation(OrientationHelper.VERTICAL) // Vertical display
R.id.textSP -> mnger.setOrientation(OrientationHelper.HORIZONTAL) // Horizontal display
}
[email protected] true
}
}
adapter.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_ap"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textSize="@dimen/dp_22" />
</LinearLayout>
adapters.kt
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
class Adapters(val textList: ArrayList<String>) : RecyclerView.Adapter<Adapters.MyViewHolder>() {
var a = 0
private var thisPosition = 0
fun getthisPosition(): Int {
return thisPosition
}
fun setThisPosition(thisPosition: Int) {
this.thisPosition = thisPosition;
}
private lateinit var litener: OnItemClickListener
fun setOnRecyclerViewItemClickListener(litener: OnItemClickListener) {
this.litener = litener;
}
interface OnItemClickListener {
fun onClick(position: Int)
fun onLongClick(position: Int)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter, parent, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int = textList.size ?: 0
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val textpos = textList[position]
holder.title.text = textpos
if (position == getthisPosition() && a == 1) {
holder.title.setBackgroundColor(Color.parseColor("#D81B60"));
} else {
holder.title.setBackgroundColor(Color.parseColor("#4F6960"));
}
//adapter Processing data in
holder.itemView.setOnClickListener {
Toast.makeText(holder.itemView.context, "${holder.title.text}", Toast.LENGTH_SHORT).show()
}
// Interface development activity Processing data in
holder.title.setOnClickListener {
if (litener != null) {
a = 1
litener.onClick(position)
}
}
holder.title.setOnLongClickListener {
if (litener != null) {
litener.onLongClick(position)
}
[email protected] true
}
}
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val title: TextView = itemView.findViewById(R.id.text_ap)
}
}
边栏推荐
- 378. The k-th smallest element in the ordered matrix ●●
- 2022-07-13 mysql/stonedb的快速子查询和慢子查询执行对比
- 弹性蛋白酶丨Worthington 核心酶详细参考资料
- 【MySQL系列】MySQL表的增删改查(基础)
- PMP考试敏捷比重猛增,如何备考?
- 2022-07-18 jenkins流水线使用及创建自己的流水线
- How to solve the "last mile of delivery" of community group purchase
- [harmony OS] [ark UI] ETS list implementation, pull-down refresh function implementation
- Redis ---- three special data types, transaction, integration, configuration file, persistent RDB and AOF, publish and subscribe, master-slave replication (sentinel mode), cache penetration and avalan
- 2022-07-15 mysql/stonedb子查询性能分析-FindOneInsidePack
猜你喜欢
Cellulase preparation scheme of Worthington plant protoplast
ADS多频功放偏置网络设计
2022-07-15 MySQL receives new connection processing
2022-07-13 mysql/stonedb subquery optimizer processing record
矽穀課堂筆記(下)
Worthington脱氧核糖核酸及相关研究工具
论文阅读 | Point-Voxel CNN for Efficient 3D Deep Learning
Pytorch deep learning practice-1-overview
[MySQL series] "once and for all" solves the problem of "inserting Chinese data" in MySQL
[harmony OS] [ark UI] ETS list implementation, pull-down refresh function implementation
随机推荐
Y73. Chapter IV Prometheus big factory monitoring system and actual combat -- blackbox exporter installation and grafana installation (IV)
【 série mysql】 "résoudre une fois pour toutes le problème de l'erreur" insérer des données chinoises "dans MySQL
[open source diary] dormitory power-off automatic light off equipment (II)
2022-07-18 反思
PMP考试敏捷比重猛增,如何备考?
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
STM32 RT thread porting lvgl
Millet box opens ADB debugging mode
Deveco studio3.0 download failed, prompting an unknown error occurred
2022-07-13 mysql/stonedb的快速子查询和慢子查询执行对比
PMP备考指南之相关事项介绍
2022-07-13 mysql/stonedb子查询-优化器处理记录
2022-07-12 使用perf统计mysql执行的性能
687. Longest same value path ●●
Solving partial differential equations and integral equations by Fourier transform
Pytorch Deep Learning Practice - 1 - Overview
Summary of optimistic lock, pessimistic lock and distributed lock
Worthington肽合成应用丨胰凝乳蛋白酶方案
2022-07-18 Jenkins deployment
滑环工作寿命如何延长