首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当需要将需要纳入视图的可组合的BringIntoViewRequester调整大小时,将无法工作

当需要将需要纳入视图的可组合的BringIntoViewRequester调整大小时,将无法工作
EN

Stack Overflow用户
提问于 2022-06-06 11:14:59
回答 1查看 794关注 0票数 1

最近几天我试图找到一个解决问题的方法

键盘与垂直滚动列中的文本输入字段重叠。

在尝试了许多事情之后,最终找到了解决方案,其中所有东西都正常工作,除非在单击按钮时,我希望在输入字段中显示错误验证消息。

在这种情况下,bringIntoViewRequester.bringIntoView()停止工作,onFocusEvent在循环中被触发。

以下是代码:

代码语言:javascript
运行
复制
@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中设置了属性

代码语言:javascript
运行
复制
android:windowSoftInputMode="adjustResize" 

在MainActivity中

代码语言:javascript
运行
复制
WindowCompat.setDecorFitsSystemWindows(window, false)
EN

回答 1

Stack Overflow用户

发布于 2022-08-04 14:25:04

好吧,这就是你所需要的,它工作得很完美:

代码语言:javascript
运行
复制
@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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72516844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档