麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 綜合 > 正文

Android中使用Kotlin實現一個簡單的登錄界面

2024-07-21 23:03:38
字體:
來源:轉載
供稿:網友

Kotlin 是一種在 Java 虛擬機上運行的靜態類型編程語言,被稱之為 Android 世界的Swift,由 JetBrains 設計開發并開源。

Kotlin 可以編譯成Java字節碼,也可以編譯成 JavaScript,方便在沒有 JVM 的設備上運行。

在Google I/O 2017中,Google 宣布 Kotlin 成為 Android 官方開發語言。

剛接觸Kotlin的第一天,仿照QQ的登錄界面,先寫一個簡單的登錄界面,雖然筆者用的不是很熟,還在慢慢摸索,但是Kotlin是真的很簡潔,筆者的實現效果如下:

kotlin,實現登錄界面,android簡單登錄界面

登錄界面代碼如下:

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實現一個簡單的登錄界面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VeVb武林網網站的支持!


注:相關教程知識閱讀請移步到kotlin教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色片网站在线免费观看 | 色七七亚洲 | 极品销魂一区二区三区 | 欧美日韩大片在线观看 | 毛片免费观看视频 | 免费黄网站在线播放 | 黄色毛片a级 | 黄色一级片在线观看 | 国产一级毛片视频在线! | 欧美一级高潮 | 午夜视频中文字幕 | 中文字幕免费在线看 | 欧美a视频在线观看 | 国产精品www | 小情侣嗯啊哦视频www | 成人啪啪色婷婷久 | 在线a亚洲视频播放在线观看 | 91精品国产综合久久婷婷香蕉 | 亚洲小视频在线 | 欧美精品色精品一区二区三区 | 91精品国产91久久久久久蜜臀 | 成人福利免费在线观看 | 88xx成人永久免费观看 | 欧美大电影免费观看 | 九九热视频免费观看 | 国产精品成人一区二区三区电影毛片 | 国产三级午夜理伦三级 | 叉逼视频 | 久久免费观看一级毛片 | 国产精品美女久久久免费 | 欧美天堂一区 | 欧美成人精品不卡视频在线观看 | 男女羞羞的视频 | caoporn国产一区二区 | 国产色爱综合网 | 免费人成在线播放 | 免费激情视频网站 | 久久91精品久久久久清纯 | 国产一区免费 | 毛片在线免费观看网址 | 亚洲网站在线 |