跳转activity
public fun goConstrantLayout(view:View){
//var intent:Intent = Intent(this,Main2Activity().javaClass)
//startActivity(intent)
var intent:Intent = Intent(this,Main2Activity::class.java)
startActivity(intent)
}
两种写法都行
获取多个view遍历
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setListeners()
}
//Kotlin 后去view遍历点击,id就是view
private fun setListeners() {
var clickableViews:List<View> = listOf(
box_one_text,box_two_text,box_three_text,box_four_text,box_five_text
)
for(item in clickableViews){
item.setOnClickListener{makeColors(it)}
}
}
private fun makeColors(view:View) {
when(view.id){
R.id.box_one_text->view.setBackgroundColor(Color.DKGRAY)
R.id.box_two_text->view.setBackgroundColor(Color.GRAY)
R.id.box_three_text->view.setBackgroundColor(Color.GREEN)
R.id.box_four_text->view.setBackgroundColor(Color.YELLOW)
R.id.box_five_text->view.setBackgroundColor(Color.RED)
else
->view.setBackgroundColor(Color.GRAY)
}
}
}
网友评论