在Jetpack中请求关注TextField撰写
问题描述:
如何在jetpack撰写中自动关注文本字段.当我单击文本字段并开始在其上键入时.同时,当我单击后退"按钮时,然后当我尝试单击文本字段时,什么也没有发生.
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 { }
}
}