Search,FocusRequester不是初始化的

问题描述 投票:0回答:3

firststate

但是我有一个错误

FocusRequester is not initialized. Here are some possible fixes: 1. Remember the FocusRequester: val focusRequester = remember { FocusRequester() } 2. Did you forget to add a Modifier.focusRequester() ? 3. Are you attempting to request focus during composition? Focus requests should be made in response to some event. Eg Modifier.clickable { focusRequester.requestFocus() } nextstate 我得出的结论是,这是由于动画性可见性所致,因为没有它,一切都起作用。也就是说,当按钮和搜索最初可见时,一切都起作用。 我认为光标无处可站立,因为它没有立即更新,但我不知道该怎么做

我的代码:

topBar = { TopAppBar( actions = { // val keyboardController = LocalSoftwareKeyboardController.current //val focusRequester = remember { FocusRequester() } var visibleSearchBar by remember { mutableStateOf(false) } var visiblecurrentSearch by remember { mutableStateOf(true) } val state = remember { mutableStateOf(TextFieldValue("")) } AnimatedVisibility(visible = visiblecurrentSearch) { IconButton(onClick = { visibleSearchBar = true visiblecurrentSearch = false //focusRequester.requestFocus() //keyboardController?.show() }) { Icon(painter = painterResource(id = R.drawable.ic_baseline_search_24), contentDescription = "search") } } AnimatedVisibility(visible = visibleSearchBar) { TextField( value = state.value, onValueChange = { value -> state.value = value }, enabled = true, shape = RoundedCornerShape(25.dp), modifier = Modifier //.focusRequester(focusRequester) .fillMaxWidth() .padding(end = 10.dp) .scale(scaleX = 1F, scaleY = 0.9F), textStyle = TextStyle(color = Color.Black), placeholder = { Text( text = "Search", fontSize = 14.sp, ) }, keyboardActions = KeyboardActions( onDone = { //movie = list } ), leadingIcon = { Icon( Icons.Default.Search, contentDescription = "", modifier = Modifier .size(20.dp) ) }, trailingIcon = { IconButton( onClick = { state.value = TextFieldValue("") = visibleSearchBar = false visiblecurrentSearch = true } ) { Icon( Icons.Default.Close, contentDescription = "", modifier = Modifier .size(20.dp) ) } }, singleLine = true, colors = TextFieldDefaults.textFieldColors( textColor = Color.Black, cursorColor = Color.Black, leadingIconColor = Color.Black, trailingIconColor = Color.Black, focusedIndicatorColor = Color.Transparent, unfocusedIndicatorColor = Color.Transparent, disabledIndicatorColor = Color.Black ) ) } }, title = { }, navigationIcon = { IconButton(onClick = {}) { Icon(painter = painterResource(id = R.drawable.ic_baseline_menu_24), contentDescription = "menu") } }, backgroundColor = Color.White, contentColor = Color.Black, elevation = 3.dp ) }
    

您可以使用副作用来要求焦点。 类似:

val focusRequester = remember { FocusRequester() } LaunchedEffect(visibleSearchBar){ if (visibleSearchBar) focusRequester.requestFocus() } AnimatedVisibility(visible = visibleSearchBar) { TextField( //... modifier = Modifier .focusRequester(focusRequester) .fillMaxWidth() .padding(end = 10.dp) .scale(scaleX = 1F, scaleY = 0.9F), //... )

我参加聚会很晚,但是我最近遇到了同样的问题...如果您使用M3,则需要与您的Textfields一起使用modifier.menuanchor() - >
https://www.composables.com/components/material3/exposeddropdownmenubox
android kotlin search android-jetpack-compose android-jetpack
3个回答
8
投票

ExposedDropdownMenuBox( expanded = expanded, onExpandedChange = { expanded = !expanded } ) { OutlineTextField( modifier = Modifier .menuAnchor(), text = text, label = label, ... more stuff

希望它可以帮助更多的人!
    

6
投票
根据建议的可能修复程序,尝试在副作用中使用FocusRequester

val focusRequester = remember { FocusRequester() } LaunchedEffect(visibleSearchBar){ if (visibleSearchBar) focusRequester.requestFocus() } topBar = { TopAppBar( actions = { // val keyboardController = LocalSoftwareKeyboardController.current //val focusRequester = remember { FocusRequester() } var visibleSearchBar by remember { mutableStateOf(false) } var visiblecurrentSearch by remember { mutableStateOf(true) } val state = remember { mutableStateOf(TextFieldValue("")) } AnimatedVisibility(visible = visiblecurrentSearch) { IconButton(onClick = { visibleSearchBar = true visiblecurrentSearch = false //focusRequester.requestFocus() //keyboardController?.show() }) { Icon(painter = painterResource(id = R.drawable.ic_baseline_search_24), contentDescription = "search") } } AnimatedVisibility(visible = visibleSearchBar) { TextField( value = state.value, onValueChange = { value -> state.value = value }, enabled = true, shape = RoundedCornerShape(25.dp), modifier = Modifier .focusRequester(focusRequester) .fillMaxWidth() .padding(end = 10.dp) .scale(scaleX = 1F, scaleY = 0.9F), textStyle = TextStyle(color = Color.Black), placeholder = { Text( text = "Search", fontSize = 14.sp, ) }, keyboardActions = KeyboardActions( onDone = { //movie = list } ), leadingIcon = { Icon( Icons.Default.Search, contentDescription = "", modifier = Modifier .size(20.dp) ) }, trailingIcon = { IconButton( onClick = { state.value = TextFieldValue("") = visibleSearchBar = false visiblecurrentSearch = true } ) { Icon( Icons.Default.Close, contentDescription = "", modifier = Modifier .size(20.dp) ) } }, singleLine = true, colors = TextFieldDefaults.textFieldColors( textColor = Color.Black, cursorColor = Color.Black, leadingIconColor = Color.Black, trailingIconColor = Color.Black, focusedIndicatorColor = Color.Transparent, unfocusedIndicatorColor = Color.Transparent, disabledIndicatorColor = Color.Black ) ) } }, title = { }, navigationIcon = { IconButton(onClick = {}) { Icon(painter = painterResource(id = R.drawable.ic_baseline_menu_24), contentDescription = "menu") } }, backgroundColor = Color.White, contentColor = Color.Black, elevation = 3.dp ) }

我还没有潜入DeepInto,但是这里不是那么好的解决方案,即使在添加了启动效果之后,仍然面临问题。
LaunchedEffect(key1 = Unit, block = { delay(200) amountField.requestFocus() })

我添加了一个杂货延迟。


1
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.