我正在接近Kotlin(和移动编码) 我正在try 将一个监听器设置为在单击多个文本视图的每个人时运行相同的函数. 我在so上找到了下面的代码(对不起,缺少链接:我又找不到了) 但是,当我试图将extView传递给该函数时,最后一个"it"出现错误.
val txtV_Ans1 = findViewById(R.id.txtV_Aswer1) as TextView
val txtV_Ans2 = findViewById(R.id.txtV_Aswer2) as TextView
val txtV_Ans3 = findViewById(R.id.txtV_Aswer3) as TextView
val txtV_Ans4 = findViewById(R.id.txtV_Aswer4) as TextView
val txtVs_Ans = arrayListOf(
txtV_Ans1,
txtV_Ans2,
txtV_Ans3,
txtV_Ans4
)
txtVs_Ans.forEach { it.setOnClickListener { txtV_Fill(it, "You clicked here")} }
fun txtV_Fill(txtV: TextView, txtToInsert: String){
txtV.text = txtToInsert
}