Kotlin 基本使用获取控件,map,list,遍历,跳转

1,不需要findbyid,直接用id名做操作即可自动引入import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        text.setOnClickListener {
            println("test")
        }
    }
}

2,list,map定义及遍历

 var list = listOf("1", "2", "3", "5")
        var map = mapOf(1 to 2, 2 to 3)
        for (item in list) {
            println(item)
        }
        for ((k, v) in map) {
            println("$k + $v + ${map[k]}")
        }
        var intent = Intent(this, Test2::class.java)
        startActivity(intent)
3,activity跳转,传值,获取值
var intent = Intent(this, Test2::class.java)
        intent.putExtra("key", "success")
        startActivity(intent)
//Test2中
var key=intent.getStringExtra("key")

你可能感兴趣的:(kotlin,Android)