kotlinandroid-jetpack-composeandroid-jetpackandroid-compose-textfieldcompose-multiplatform

How to make a text clickable in Jetpack Compose? And also toggle it to non-clickable after selecting once


This is my code:

Text(
    text = "Resend OTP",
    fontSize = 20.sp,
    color =  Textfieldcolor,
    style = TextStyle(textDecoration = TextDecoration.Underline)
)

I want the text to be clickable once and then disabled.

How do I do this?


Solution

  • You can add the clickable modifier to your Text or use ClickableText instead of Text.

    Here is an example of how to do it with ClickableText:

    var enabled by remember { mutableStateOf(true)}
    
    ClickableText(
        text = AnnotatedString(text) ,
        onClick = {
            if (enabled) {
                enabled = false
                text = "Disabled"
            }
        })