1,不需要findbyid,直接用id名做操作即可自动引入import kotlinx.android.synthetic.main.activity_main.*
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="@color/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
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")