你好我们点击按钮有问题
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)
}
}
但我的按钮不起作用。我的问题在哪里?
你不需要定义函数声明(fun
),试试这个:
singupButton.setOnClickListener {view ->
val intent = Intent(applicationContext,createUser::class.java)
startActivity(intent)
}
要不就
singupButton.setOnClickListener {
val intent = Intent(applicationContext,createUser::class.java)
startActivity(intent)
}
这是一个基本的样本
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()
}
你的问题是,你在点击监听器中定义了一个函数,你没有调用它。
你原来的代码:
singupButton.setOnClickListener {
fun crtUser (view: View) {
val intent = Intent(applicationContext,createUser::class.java)
startActivity(intent)
}
}
你应该调用这个函数:
singupButton.setOnClickListener { view ->
fun crtUser (view: View) {
val intent = Intent(applicationContext,createUser::class.java)
startActivity(intent)
}
crtUser(view)
}
或者不要定义这个函数,只需调用它:
singupButton.setOnClickListener {
val intent = Intent(applicationContext,createUser::class.java)
startActivity(intent)
}