Android Compose中TextField被键盘遮挡。

4

我在列中有一个带有verticalScroll()TextField。 当添加大量字符时,文本字段大小超过键盘范围,我无法看到自己正在输入什么。 我尝试使用这个库,但并没有帮助。

2个回答

4

我认为你可以在TextField中使用BringIntoViewRequester

var state by rememberSaveable {
    mutableStateOf("")
}
val coroutineScope = rememberCoroutineScope()
val bringIntoViewRequester = remember {
    BringIntoViewRequester()
}

TextField(
    value = state,
    onValueChange = { text ->
        state = text
        // This will cause the TextField be repositioned on the screen
        // while you're typing
        coroutineScope.launch {
            bringIntoViewRequester.bringIntoView()
        }
    },
    modifier = Modifier
        .bringIntoViewRequester(bringIntoViewRequester)
        .onFocusChanged {
            if (it.isFocused) {
                coroutineScope.launch {
                    delay(400) // delay to way the keyboard shows up
                    bringIntoViewRequester.bringIntoView()
                }
            }
        },
)

点击此处查看完整示例代码。


谢谢,那个延迟语句修复了我一直看到的一些奇怪行为。 - TheYogi
我们为什么需要延迟? - sarthak gupta

-2

您可以在文本的XML中添加android:ellipsize="end"android:maxLines="1"或者您需要的行数,希望对您有所帮助。


我使用Compose,Compose可以在没有XML的情况下正常工作 :) - Tus Die

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接