当前位置:网站首页>Kotlin练手,以登录为例,Anko简单使用
Kotlin练手,以登录为例,Anko简单使用
2022-06-10 12:54:00 【yechaoa】
在Kotlin还不是正房的时候就听说过了,除了Java之外也能开发Android APP的一种语言,小众到鲜为人知,甚至介绍都不多。但是在谷歌大会宣布kotlin为官方语言之后,铺天盖地的都是关于kotlin的介绍和语法简介,出身豪门等等。 后来,J神也离开了Square去了Google,而且是Kotlin项目组。 到最近Kotlin1.2版本发布又是一波大范围的推送,而且在GitHub的排名也不断上升。 Google一系列的动作都在加快Kotlin在Android领域取代Java的速度。甚至还有跨平台的骚操作。。
Kotlin官方文档(英文版)
Kotlin官方文档(中文版)
Anko——JetBrains开发的一个强大的库
以登录为例,小小练手
package com.yechaoa.kotlinforlogin
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.*
// : 表示继承,类型声明等
class LoginActivity : AppCompatActivity(), AnkoLogger {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
//不需要分号
//apply plugin: 'kotlin-android-extensions'
//添加插件可以直接拿xml文件中的id
btn_login.setOnClickListener {
//字符串拼接
Log.i("tag", "${getUsername()}+${getPassword()}")
if ("123" == (getUsername()) && "123" == (getPassword()))
showDialog()
else
Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show()
}
}
private fun showDialog() {
alert("登录成功", "提示") {
yesButton {
//this指向是谁, ::class.java加载java文件
startActivity(Intent([email protected], MainActivity::class.java))
}
noButton {}
}.show()
}
//自定义函数
private fun getUsername(): String {
//定义变量,? 表示可空
val username = til_username.editText?.text.toString().trim()
return if (TextUtils.isEmpty(username))
""
else
username
}
private fun getPassword(): String {
val password = til_password.editText?.text.toString().trim()
return if (TextUtils.isEmpty(password))
""
else
password
}
override fun onDestroy() {
super.onDestroy()
//AnkoLogger
info("onDestroy")
}
}另外,Anko支持动态生成视图,语法简单,结构清晰,但是不能预览,预览插件:
https://github.com/Kotlin/anko/wiki/Anko-Layouts#anko-support-plugin
Java 代码转换为 Kotlin
ctrl +alt + shift + k
边栏推荐
- Qt数据库应用22-文件编码格式识别
- If I write the for loop again, I will hammer myself
- Cvpr2022|aconvnetforthe2020s & how to design neural network Summary
- Stereo vision based semantic 3D object and ego motion tracking for automotive driving
- 3. 网页开发工具 VS Code
- Find the number of common subsequences of K strings
- Neuron Newsletter 2022 - 05 | ajout de 2 entraînements Sud et 1 Application Nord, mise en œuvre de l'extension personnalisée par Modbus TCP
- Recommend an efficient IO component - okio
- 【NLP】NLP全路径学习推荐
- 常识,神经元数量,小鼠的脑内神经元大约在7000万个、人类约有860亿个
猜你喜欢

從解讀 BDC 自動生成的代碼談起,講解 SAPGUI 的程序組成部分

Introduction of Altium Designer

13、 System call and shell (freesanding shell, terminal and job control)

How does the API detect security configuration errors?
![buuctf [PHP]inclusion](/img/02/d328ed84e4641c09c5b1eba3ac6ea9.png)
buuctf [PHP]inclusion

MySQL master database operation large table DDL, slave database crash and system parameter error setting

Vdo-slam: a visual dynamic object aware slam system paper reading

JS click the button to slide to the left

汇编语言入门-总结

The deep neural network classifies nearly 2billion images per second, and the new brain like optical classifier chip is on nature
随机推荐
Neuron Newsletter 2022-05|新增 2 個南向驅動和 1 個北向應用、Modbus TCP 實現定制擴展
'setbackgrounddrawable() 'is deprecated, setbackgrounddrawable is obsolete
buuctf [Jupyter]notebook-rce
JS prohibit copying page content
Error:top-left corner pixel must be either opaque white or transparent.
Sohu employees encounter wage subsidy fraud. What is the difference between black property and gray property and how to trace the source?
[spark] (task8) pipeline channel establishment in sparkml
WTO MC12 restart agenda focuses on global economic recovery
[deep learning] the credit card fraud anomaly detection based on the deep learning autoencoder is very effective
'getWidth()' is deprecated,'getHeight()' is deprecated
2022 ciscn preliminary satool
Six stone programming: talking about naming based on the position of word processing
change system time
【FLinlk】Flink小坑之kerberos动态认证
Recommend an efficient IO component - okio
Baidu programmers were sentenced to nine months for deleting the database. The one click unbinding function of the mobile phone number was released. Twitter compromised with musk again. Today, more bi
Cvpr2022|aconvnetforthe2020s & how to design neural network Summary
"Reduce the burden" so that the "pig" can fly higher
如何才能把团队给带解散。。。
Google proposed the super pre training model coca, and the accuracy of fine-tuning top-1 on Imagenet reached 91%! SOTA on multiple downstream tasks!