如何使用OnClickListener创建简单的事件//更改文本或颜色

//告诉我哪里出错了,我试图通过点击按钮来改变文本

val buttonMoto: Button = (findViewById<Button>(R.id.buttonMoto)) buttonMoto.setOnClickListener(View.OnClickListener { fun OnClick(view: View){ val textviewMoto: TextView = (findViewById<TextView>(R.id.textViewMoto)) textviewMoto.text = "Hello World" 

如果你想创建onClick,这是非常简单的kotlin。

 textview.onClick{ } 

为什么不使用

 if (view instanceof TextView) view.text = "Hello World"; 

甚至只是

 view.text = "Hello world"; 

Jehad,如果你只是想改变一些按钮上的文字,只需像这样用lambda函数来使用它

  findViewById<Button>(R.id.button).setOnClickListener { val textviewMoto = (findViewById<TextView>(R.id.textViewMoto)) textviewMoto.text = "Hello World" }