Kotlin setOnclickListener按钮不起作用

Cer*_*glu 3 kotlin android-studio

大家好,我在点击按钮时遇到问题

    fun mainPage(view: View) {
            val intent = Intent(applicationContext, MainActivity::class.java)
            intent.putExtra("input", userText.text.toString())
            startActivity(intent)
        }

       //second button started in here
         singupButton.setOnClickListener {
            fun crtUser (view: View) {
                val intent = Intent(applicationContext,createUser::class.java)
                startActivity(intent)
            }
        }
Run Code Online (Sandbox Code Playgroud)

但我的按钮不起作用。我的问题在哪里?

Jor*_*sys 5

您无需定义函数声明(fun),请尝试以下操作:

singupButton.setOnClickListener {view ->
                val intent = Intent(applicationContext,createUser::class.java)
                startActivity(intent)
 }
Run Code Online (Sandbox Code Playgroud)

要不就

singupButton.setOnClickListener {
                  val intent = Intent(applicationContext,createUser::class.java)
                  startActivity(intent)
}
Run Code Online (Sandbox Code Playgroud)

这是一个基本样本

val myButton = findViewById<Button>(R.id.myButton) as Button
    //set listener
    myButton.setOnClickListener {
        //Action perform when the user clicks on the button.
        Toast.makeText(this@MainActivity, "You clicked me.", Toast.LENGTH_SHORT).show()
    }
Run Code Online (Sandbox Code Playgroud)