资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在Android中使用Kotlin实现一个登录界面

怎么在Android中使用Kotlin实现一个登录界面?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联公司自2013年起,公司以成都网站设计、做网站、系统开发、网络推广、文化传媒、企业宣传、平面广告设计等为主要业务,适用行业近百种。服务企业客户成百上千,涉及国内多个省份客户。拥有多年网站建设开发经验。为企业提供专业的网站建设、创意设计、宣传推广等服务。 通过专业的设计、独特的风格,为不同客户提供各种风格的特色服务。

登录界面代码如下:

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 {
    override fun createView(ui: AnkoContext) = 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() 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将会更加的简洁明了,省去了定义变量和查找布局文件的操作,大大解放了我们程序员;

下面的代码所示是笔者使用布局文件实现的布局效果,和上面的效果一样,但是会复杂很多,大家可以自己自己体会一下;



  
  
    
    
  
  
    
    
  
  

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


当前题目:怎么在Android中使用Kotlin实现一个登录界面
转载源于:http://cdkjz.cn/article/iechjg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220