onClick not working correctly in Jetpack Compose

huangapple go评论73阅读模式
英文:

onClick not working correctly in Jetpack Compose

问题

I'm trying to make a button onClick show a message in Jetpack Compose, but it just doesn't work. It seems like if I wasn't pressing it.

Here's what I've tried:

Button(
onClick = {
Toast.makeText(this@MainActivity, "Clicked!", Toast.LENGTH_SHORT).show()
Log.d("Auth:Auth:", "Auth: true")
//biometricPrompt.authenticate(biometricPromptInfo)
},
colors = ButtonDefaults.buttonColors(
backgroundColor = androidx.compose.ui.graphics.Color(0xFF03A278),
contentColor = androidx.compose.ui.graphics.Color.White
)
) {
Log.d("Auth:Auth:", "Auth: text")
Text(getString(R.string.authenticate_button))
}

The logd message above "Text" is showing, but not the one under "Toast". The toast isn't showing either.

Also, when I click on it I get these messages on Logcat:
[Logcat messages]

英文:

I'm trying to make a button onClick show a message in Jetpack Compose, but it just doesn't work. It seems like if I wasn't pressing it.

Here's what I've tried:

Button(
                onClick = {
                    Toast.makeText(this@MainActivity, "Clicked!", Toast.LENGTH_SHORT).show()
                    Log.d("Auth:Auth:", "Auth: true")
                    //biometricPrompt.authenticate(biometricPromptInfo)
                },
                colors = ButtonDefaults.buttonColors(
                    backgroundColor = androidx.compose.ui.graphics.Color(0xFF03A278),
                    contentColor = androidx.compose.ui.graphics.Color.White
                )
            ) {
                Log.d("Auth:Auth:", "Auth: text")
                Text(getString(R.string.authenticate_button))
            }

The logd message above "Text" is showing, but not the one under "Toast". The toast isn't showing either.

Also, when I click on it I get these messages on Logcat:
Logcat messages

答案1

得分: 1

在你的类或活动中,将上下文传递为 this:

val context = LocalContext.current.applicationContext

或者你也可以这样做:

val context = LocalContext.current

这两种方式都可以正常工作。

英文:

Yes in your class or activity pass the context as this:

val context = LocalContext.current.applicationContext

Or you can also do this:

val context = LocalContext.current

Both will work fine

答案2

得分: 0

已解决,有一个弹出式旋转器存在并搞乱了一切。

英文:

Solved it, there was a PopUpSpinner hanging around and messing it all up.

答案3

得分: 0

我认为你没有正确使用toast。而且我不记得我们可以传递this@MainActivity或者你正在使用的任何其他Activity,它会报错。要传递上下文,请执行以下操作:

val context = LocalContext.current.applicationContext

在onClick中执行以下操作:

Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show()
英文:

I think you are not using toast in correct way. And I don't remember that we can pass
this@MainActivity or any other Activity in which you are it will give error.
To pass context do this:

val context = LocalContext.current.applicationContext

And in onClick do this:

Toast.makeText(context, "Clicked!", Toast.LENGTH_SHORT).show()

huangapple
  • 本文由 发表于 2023年6月26日 22:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定