Kotlin学习(二)

q.png

用Kotlin写一个登录的操作

布局文件

<?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="com.chinamall21.mobile.kotlin.MainActivity">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">

        <EditText
            android:id="@+id/et_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="用户名"/>
    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="密码"
            android:inputType="textPassword"/>

    </android.support.design.widget.TextInputLayout>


    <Button
        android:id="@+id/bt_login"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="20dp"
        android:background="@color/colorAccent"
        android:text="登录"
        android:textColor="#fff"/>

    <Button
        android:id="@+id/bt_register"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@color/colorAccent"
        android:text="注册"
        android:textColor="#fff"/>

</LinearLayout>

直接可以用布局中控件定义的id值来使用

 bt_login.setOnClickListener(this)
 bt_register.setOnClickListener(this)

点击事件

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.bt_login ->
                if (et_username.text.toString().isEmpty() || et_password.text.toString().isEmpty()) {
                    Toast.makeText(this, "please input username or password", Toast.LENGTH_SHORT).show()
                } else
                    Toast.makeText(this, "Login", Toast.LENGTH_SHORT).show()

            R.id.bt_register -> Toast.makeText(this, "Register", Toast.LENGTH_SHORT).show()
        }

    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,613评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,524评论 2 59
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,909评论 0 17
  • 清晨,我站在窗前,推开玻璃,让清凉的秋风吹纱窗而过,一屋子深秋的味道,落叶又铺满了窗前的小路,黄的绿的,以潇洒或狼...
    半个城市阅读 1,459评论 0 0
  • 很高兴又到了给好种子浇水施肥的时候了! 我99天的践行目标是家庭收入增加200万元。 业力伙伴赵琳的99...
    绚风阅读 1,774评论 0 2

友情链接更多精彩内容