Android Kotlin Activity笔记

  1. 使用Anko,简化代码
build.gradle 依赖 anko
implementation 'org.jetbrains.anko:anko-common:+'
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn.text ="按钮1"
        btn.setOnClickListener(this)
        btn2.setOnClickListener(this)
    }

    override fun onClick(view: View) {
        val id = view.id
        when (id) {
            R.id.btn -> btnClick()
            R.id.btn2 ->{
                toast("按钮2点击")
            }
            else -> {

            }
        }
    }

    fun btnClick() {
        toast("按钮点击")
    }
}

2.页面跳转

 val intent = Intent(this@MainActivity,PayssionActivity::class.java)
 startActivity(intent)

你可能感兴趣的:(Android Kotlin Activity笔记)