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实现一个简单的登录界面,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android中使用Kotlin实现一个简单的登录界面全部内容,希望文章能够帮你解决Android中使用Kotlin实现一个简单的登录界面所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)