当前位置:网站首页>安卓 kotlin 的简单使用
安卓 kotlin 的简单使用
2022-07-22 01:43:00 【谈情不如逗狗.】
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) -> "账号不能为空!"
TextUtils.isEmpty(password) -> "密码不能为空!"
!TextUtils.equals(userName, "542210035") -> "账号不正确!"
!TextUtils.equals(password, "123456") -> "密码不正确!"
else -> "登录成功"
}
Toast.makeText(this, value, Toast.LENGTH_LONG).show()
if (TextUtils.equals(value, "登录成功")) {
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="垂直显示"
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="水平显示"
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) //垂直显示
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接口实现
*/
val obj = object : OnItemClickListener {
override fun onLongClick(position: Int) {
Log.i("TAG", "长按:${textList.get(position)}")
adapter.setThisPosition(position)
adapter.notifyDataSetChanged()
}
override fun onClick(position: Int) {
Log.i("TAG", "点击:${textList.get(position)}")
adapter.setThisPosition(position)
adapter.notifyDataSetChanged()
}
}
/**
* 括号里面 ,并用lambda表达式表示
*/
@SuppressLint("WrongConstant")
var clinc1 = View.OnClickListener({ v ->
when (v.id) {
R.id.textCZ -> mnger.setOrientation(OrientationHelper.VERTICAL) //垂直显示
R.id.textSP -> mnger.setOrientation(OrientationHelper.HORIZONTAL) //水平显示
}
})
/**
* 删除(),并用lambda表达式表示
*/
var clinc2 = View.OnClickListener { v ->
when (v.id) {
R.id.textCZ -> Log.i("TAG", "垂直")
R.id.textSP -> Log.i("TAG", "水平")
}
}
//删除(),删除lambda表达式, 只有一个参数可以用it表示
var clinc3 = View.OnClickListener {
when (it.id) {
R.id.textCZ -> Log.i("TAG", "垂直")
R.id.textSP -> Log.i("TAG", "水平")
}
}
/**
* 长按事件
*/
@SuppressLint("WrongConstant")
var clinc6 = View.OnLongClickListener() [email protected]{
when (it.id) {
R.id.textCZ -> mnger.setOrientation(OrientationHelper.VERTICAL) //垂直显示
R.id.textSP -> mnger.setOrientation(OrientationHelper.HORIZONTAL) //水平显示
}
[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 中处理数据
holder.itemView.setOnClickListener {
Toast.makeText(holder.itemView.context, "${holder.title.text}", Toast.LENGTH_SHORT).show()
}
//接口拓展 activity 中处理数据
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)
}
}
边栏推荐
猜你喜欢
再见Attention:建模用户长期兴趣的新范式
2022-07-15 mysql/stonedb子查询性能分析-FindOneInsidePack
MSTP&VRRP协议
2022-07-18 子查询-优化exists场景的join查询优化器的处理
论文阅读 | Point-Voxel CNN for Efficient 3D Deep Learning
2022.7.11-7.17 AI industry weekly (issue 106): just try your best
【Harmony OS】【ARK UI】【Demo】加载动画实现
【 série mysql】 "résoudre une fois pour toutes le problème de l'erreur" insérer des données chinoises "dans MySQL
Notes de classe de la Silicon Valley (Partie 2)
【MySQL系列】“一勞永逸“ 解决MySQL中 “插入中文數據“出錯的問題
随机推荐
2022-07-15 mysql/stonedb子查询校验exists流程分析
Network layer protocol -------- IP
[HMS core] [FAQ] HMS toolkit typical problem set 1
[HMS core] [FAQ] collection of typical problems of account kit, MDM capability and push Kit 6
AOSP ~ Camera - RK HAL3 ( 二 )
[HMS core] [FAQ] collection of typical problems of sports health, audio editing and Huawei account service 7
QProcess类
[MySQL series] addition, deletion, modification and query of MySQL tables (Advanced)
论文阅读 | Point-Voxel CNN for Efficient 3D Deep Learning
2022-07-20 MySQL hashjoin description
2022-07-13 mysql的查询优化分析
Tencent cloud deploys Devops
[harmonyos] [FAQ] Hongmeng problem collection 4
Hcia-r & s self use notes (13) case analysis of typical routing problems and overview of dynamic routing
PMP Exam agile proportion soared, how to prepare for the exam?
硅谷课堂笔记(下)
再见Attention:建模用户长期兴趣的新范式
入门推荐系统必读的10篇baseline paper
Just finished the hot PCA National Certificate and lost a full score. Company B dare not come out, afraid of being hammered by NB company below..
某东分析样本