请求在 jetpack compose 中关注 TextField [英] Request Focus on TextField in jetpack compose

查看:30
本文介绍了请求在 jetpack compose 中关注 TextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 jetpack compose 中自动聚焦文本字段.当我单击文本字段并开始在其上键入时.同时,当我单击后退按钮时,然后当我尝试单击文本字段时,没有任何反应.

How to auto focus on textfield in jetpack compose. When i click on textfield and start typing on it. Meantime when i click back button,then when i'm try to click on textfield, nothing happens.

val textState = remember { mutableStateOf(TextFieldValue()) }
TextField(
    modifier = Modifier.fillMaxWidth(),
    value = textState.value,
    onValueChange = { value : TextFieldValue ->
        textState.value = value
    }
)

推荐答案

发布问题的更新答案(API 在 Compose Beta 中重命名)

Posting an updated Answer to the question (APIs were renamed in Compose Beta)

@Composable
fun AutoFocusingText() {
    var value by mutableStateOf("Enter Text")
    val focusRequester = remember { FocusRequester() }
    TextField(
        value = value, 
        onValueChange = { value = it },
        modifier = Modifier.focusRequester(focusRequester)
    )
    
    DisposableEffect(Unit) {
        focusRequester.requestFocus()
        onDispose { }
    }
}

这篇关于请求在 jetpack compose 中关注 TextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆