Kotlin學(xué)習(xí)(二)

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)

點(diǎn)擊事件

    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()
        }

    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,940評論 25 709
  • 用兩張圖告訴你,為什么你的 App 會卡頓? - Android - 掘金 Cover 有什么料? 從這篇文章中你...
    hw1212閱讀 13,953評論 2 59
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,321評論 0 17
  • 清晨,我站在窗前,推開玻璃,讓清涼的秋風(fēng)吹紗窗而過,一屋子深秋的味道,落葉又鋪滿了窗前的小路,黃的綠的,以瀟灑或狼...
    半個城市閱讀 215評論 0 0
  • 很高興又到了給好種子澆水施肥的時候了! 我99天的踐行目標(biāo)是家庭收入增加200萬元。 業(yè)力伙伴趙琳的99...
    絢風(fēng)閱讀 275評論 0 2

友情鏈接更多精彩內(nèi)容