最近几天我试图找到一个解决问题的方法
键盘与垂直滚动列中的文本输入字段重叠。
在尝试了许多事情之后,最终找到了解决方案,其中所有东西都正常工作,除非在单击按钮时,我希望在输入字段中显示错误验证消息。
在这种情况下,bringIntoViewRequester.bringIntoView()停止工作,onFocusEvent在循环中被触发。
以下是代码:
@Composable
fun MainScreen() {
Column(
Modifier
.fillMaxSize()
.verticalScroll(state = scrollState)
.imePadding()
.navigationBarsPadding()
) {
InputField()
InputField()
InputField()
....
}
}
@Composable
fun InputField() {
val bringIntoViewRequester = remember { BringIntoViewRequester() }
val coroutineScope = rememberCoroutineScope()
val focusLocalManager = LocalFocusManager.current
val view = LocalView.current
val context = LocalContext.current
WindowInsets.ime
LaunchedEffect(Unit) {
ViewCompat.setWindowInsetsAnimationCallback(view, null)
}
Column(
modifier = Modifier
.wrapContentHeight()
.bringIntoViewRequester(bringIntoViewRequester)
) {
BasicTextField(
modifier = Modifier
.onFocusEvent { fs ->
if (fs.isFocused) {
coroutineScope.launch {
bringIntoViewRequester.bringIntoView()
}
}
}
.....
)
if (errorMessage.isNotEmpty())
Text(
text = errorMessage,
fontSize = 16.sp,
color = MaterialTheme.colors.error
)
}
}
此外,我还在AndroidManifest.xml中设置了属性
android:windowSoftInputMode="adjustResize"
在MainActivity中
WindowCompat.setDecorFitsSystemWindows(window, false)
发布于 2022-08-04 14:25:04
好吧,这就是你所需要的,它工作得很完美:
@ExperimentalMaterial3Api
@ExperimentalFoundationApi
@Composable
fun OutlinedTextFieldValidation(
value: String,
onValueChange: (String) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
readOnly: Boolean = false,
textStyle: TextStyle = LocalTextStyle.current,
label: @Composable (() -> Unit)? = null,
placeholder: @Composable (() -> Unit)? = null,
leadingIcon: @Composable (() -> Unit)? = null,
error: String? = null,
isError: Boolean = error?.isNotEmpty() ?: false,
trailingIcon: @Composable (() -> Unit)? = {
if (error?.isNotEmpty() == true)
Icon(Icons.Filled.Warning, "error", tint = MaterialTheme.colorScheme.error)
},
visualTransformation: VisualTransformation = VisualTransformation.None,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
singleLine: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = MaterialTheme.shapes.small,
colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors(
disabledTextColor = Color.Black
),
) {
Column(
modifier = modifier.imePadding(), // <-- this will save your life
) {
OutlinedTextField(
enabled = enabled,
readOnly = readOnly,
value = value,
onValueChange = onValueChange,
modifier = modifier.fillMaxWidth(),
singleLine = singleLine,
textStyle = textStyle,
label = label,
placeholder = placeholder,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
isError = isError,
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
maxLines = maxLines,
interactionSource = interactionSource,
shape = shape,
colors = colors,
)
if (error?.isNotEmpty() == true) {
Text(
text = error,
color = MaterialTheme.colorScheme.error,
style = MaterialTheme.typography.labelSmall,
modifier = Modifier.padding(start = 16.dp, top = 0.dp),
)
}
}
}
特别感谢Rafa
https://stackoverflow.com/questions/72516844
复制相似问题