大家好,我点击按钮
有问题 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)
}
}
但我的按钮不起作用。我的问题在哪里?
答案 0 :(得分:4)
你不需要定义一个函数声明(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()
}
这是一个基本样本
jshell> /set editor c:\sublime3\sublime_text.exe
答案 1 :(得分:0)
问题是,你在点击监听器中定义了一个函数,你没有调用它。
您的原始代码:
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)
}