Android中使用Kotlin实现一个简单的登录界面
作者:neuHenry 发布时间:2023-01-29 13:51:37
标签:android,kotlin,登录
Kotlin 是一种在 Java 虚拟机上运行的静态类型编程语言,被称之为 Android 世界的Swift,由 JetBrains 设计开发并开源。
Kotlin 可以编译成Java字节码,也可以编译成 JavaScript,方便在没有 JVM 的设备上运行。
在Google I/O 2017中,Google 宣布 Kotlin 成为 Android 官方开发语言。
刚接触Kotlin的第一天,仿照QQ的登录界面,先写一个简单的登录界面,虽然笔者用的不是很熟,还在慢慢摸索,但是Kotlin是真的很简洁,笔者的实现效果如下:
登录界面代码如下:
class LoginActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_login)
LoginUi().setContentView(this@LoginActivity)
}
lateinit var et_account: EditText
lateinit var et_password: EditText
inner class LoginUi : AnkoComponent<LoginActivity> {
override fun createView(ui: AnkoContext<LoginActivity>) = with(ui) {
verticalLayout {
backgroundColor = context.resources.getColor(android.R.color.white)
gravity = Gravity.CENTER_HORIZONTAL
imageView(R.drawable.touxiang).lparams {
width = dip(100)
height = dip(100)
topMargin = dip(64)
}
linearLayout {
gravity = Gravity.CENTER_VERTICAL
orientation = HORIZONTAL
backgroundResource = R.drawable.bg_frame_corner
imageView {
image = resources.getDrawable(R.mipmap.ic_username)
}.lparams(width = wrapContent, height = wrapContent) {
leftMargin = dip(12)
rightMargin = dip(15)
}
et_account = editText {
hint = "登录账户"
hintTextColor = Color.parseColor("#666666")
textSize = 14f
background = null
}.lparams {
topMargin = dip(5)
}
}.lparams(width = dip(300), height = dip(40)) {
topMargin = dip(30)
}
linearLayout {
gravity = Gravity.CENTER_VERTICAL
orientation = HORIZONTAL
backgroundResource = R.drawable.bg_frame_corner
imageView {
image = resources.getDrawable(R.mipmap.ic_password)
}.lparams(width = wrapContent, height = wrapContent) {
leftMargin = dip(12)
rightMargin = dip(15)
}
et_password = editText {
hint = "账户密码"
hintTextColor = Color.parseColor("#666666")
textSize = 14f
background = null
}.lparams {
topMargin = dip(5)
}
}.lparams {
width = dip(300)
height = dip(40)
topMargin = dip(10)
}
button("登录") {
gravity = Gravity.CENTER
background = resources.getDrawable(R.drawable.bg_login_btn)
textColor = Color.parseColor("#ffffff")
textSize = 18f
onClick {
if (et_account.text.toString().isNotEmpty() && et_password.text.toString().isNotEmpty())
startActivity<MainActivity>() else toast("请输入账户或者密码")
}
}.lparams(width = dip(300), height = dip(44)) {
topMargin = dip(18)
}
linearLayout {
orientation = HORIZONTAL
gravity = Gravity.CENTER_VERTICAL
checkBox("记住密码") {
textColor = Color.parseColor("#666666")
textSize = 16f
leftPadding = dip(5)
}
textView("新用户注册") {
textColor = Color.parseColor("#1783e3")
gravity = Gravity.RIGHT
textSize = 16f
}.lparams(width = matchParent)
}.lparams(width = dip(300)) {
topMargin = dip(18)
}
textView("Copyright © Henry") {
textSize = 14f
gravity = Gravity.CENTER or Gravity.BOTTOM
}.lparams {
bottomMargin = dip(35)
weight = 1f
}
}
}
}
override fun onClick(v: View) {
when (v.id) {
}
}
}
实现出来的效果和我们设置布局文件所实现的效果一样,但是相比使用布局文件来说,使用Kotlin将会更加的简洁明了,省去了定义变量和查找布局文件的操作,大大解放了我们程序员;
下面的代码所示是笔者使用布局文件实现的布局效果,和上面的效果一样,但是会复杂很多,大家可以自己自己体会一下;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#ffffff"
android:orientation="vertical"
android:padding="40dp">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/touxiang"
app:civ_border_color="#FF000000"
app:civ_border_width="2dp" />
<LinearLayout
android:id="@+id/lin_count"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="45dp"
android:background="@drawable/bg_frame_corner"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="15dp"
android:src="@drawable/count" />
<EditText
android:id="@+id/loginAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="登录账户"
android:maxLength="11"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/lin_password"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/bg_frame_corner"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="15dp"
android:src="@drawable/password"/>
<EditText
android:id="@+id/loginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="账户密码"
android:password="true"
android:maxLength="11"
android:textSize="16sp"/>
</LinearLayout>
<Button
android:id="@+id/login_button"
android:layout_width="300dp"
android:layout_height="44dp"
android:layout_gravity="center"
android:background="@drawable/bg_login_btn"
android:layout_marginTop="18dp"
android:text="登录"
android:textColor="#ffffff"
android:textSize="18sp" />
<LinearLayout
android:id="@+id/lin_remember"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/rem_pas_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="记住密码"
android:textColor="#666666"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="新用户注册"
android:textColor="#1783e3"
android:textSize="16sp" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="35dp"
android:layout_weight="1"
android:gravity="bottom|center"
android:text="Copyright © Henry"
android:textSize="14sp" />
</LinearLayout>
总结
以上所述是小编给大家介绍的Android Kotlin实现一个简单的登录界面网站的支持!
来源:http://blog.csdn.net/u010347226/article/details/78089590


猜你喜欢
- LRU缓存替换策略缓存是一种非常常见的设计,通过将数据缓存到访问速度更快的存储设备中,来提高数据的访问速度,如内存、CPU缓存、硬盘缓存等。
- 我们首先看一下效果图,有个整体的印象好了,为了便于理解,这里就按照动画所见内容依次展开来说准备这里决定采用canvas(画布)和paint(
- 在传统的单服务架构中,一般来说,只有一个服务器,那么不存在 Session共享问题,但是在分布式/集群项目中,Session 共享则是一个必
- 前言这个东西有啥用,好玩?确实, 好玩归好玩,其实很有使用场景。可以自己选则一些业务节点触发这个机器人助手的消息推送;简单举例:1. 有人给
- “Java is still not dead—and people are starting to figure that out.”本教
- 目录对zygote的理解作用启动流程启动入口脚本讲解启动过程App_main::mainAndroidRuntime::start对zygo
- 1、什么是 生命周期?Maven 强大的原因是有一个十分完善的生命周期,生命周期可以理解为项目构建步骤的集合,它定义了各个构建环节的执行顺序
- 在介绍程序实现之前,我们先看下Android中Activities和Task的基础知识。我们都知道,一个Activity 可以启动另一个Ac
- package GraphicsCanvas;import java.awt.BorderLayout;import java.awt.Ca
- 前言Android6.0代号棉花糖。尽管是在15年I/O大会上Google被正式发布的了。但是看看大多数人的项目中大家的 targetSdk
- 本文将带领大家使用基于JAX-RS REST风格的实现Jersey来上传文件到服务器制定的文件夹,如果是图片并读取显示出该图片。准备工作:准
- 目录线程同步的几种方法:阻塞加锁(lock)Monitors互斥锁(Mutex)信号和句柄InterlockedReaderWriterLo
- 本文实例讲述了Android实现手机振动设置的方法。分享给大家供大家参考。具体如下:main.xml布局文件:<?xml versio
- 先上代码,再来分析public class FileDownloadList {/**上下文*/ private Context mCont
- 前面一篇文章我们介绍了使用CAS算法实现的非阻塞队列ConcurrentLinedQueue, 下面我们来介绍使用独占锁实现的阻塞队列Lin
- 可能也有其他方法,比如用 WGet 等等,但是 推荐用 PowerShell ,为什么呢,因为 PowerShell 太强大呗PowerSh
- 在项目中,我们要求做一个纸飞机的功能:就是当打开这个界面时,会有4架纸飞机从屏幕左侧飞入,然后到达自己的位置坐上下浮动,同时云彩也不断地从屏
- 文件移动是计算机资源管理常用的一个操作,这在操作系统中可以通过文件的剪切与复制或鼠标拖动来实现。但是在Java文件的编程实现中,大多是通过复
- 1.使用java.util.Properties类的load()方法示例:Java代码InputStream in = lnew Buffe
- 强调一下阅读系统源码,起码要对进程间通信要了解,对binder机制非常非常清楚,binder就是指南针,要不然你会晕头转向;强行阅读,就容易