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

时间:2017-11-03 19:25:04

标签: android android-studio kotlin

//告诉我哪里出错了,我试图点击按钮更改文字

    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"

3 个答案:

答案 0 :(得分:1)

如果你想创建onClick,这对于kotlin来说非常简单。

textview.onClick{   }

答案 1 :(得分:0)

为什么不使用

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

甚至只是

view.text = "Hello world";

答案 2 :(得分:0)

Jehad,如果您只是想点击按钮上的某些文字 只需像lambda函数一样使用它

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