聚焦模式下无法更改光标位置

时间:2020-09-28 19:34:29

标签: android kotlin android-jetpack-compose

我有一个对话框和一个可编辑的文本字段。一切正常,但是当我开始编辑字段时我无法更改光标位置。我必须按下“后退”按钮,然后将其再次聚焦在正确的位置。有什么想法吗?

@Composable
inline fun DialogInputText(display: MutableState<Boolean>, text: String, title: String, noinline onTextSet: (String)->Unit){

    BaseDialog(display = display ) {

        var text by remember { mutableStateOf(TextFieldValue(text)) }

        DialogHeader(title = title)


        EditText(text, { text = it}, modifier = Modifier.padding(8.dp).height(30.dp))

        DialogButtons {
            TextButton(onClick = {display.value = false} ) {
                Text(text = "ZPĚT")
            }

            TextButton(onClick = {onTextSet.invoke(text.text)}) {
                Text(text = "POKRAČOVAT")
            }
        }
    }

}
@Composable
fun EditText(
    text: TextFieldValue,
    onValueChange: (TextFieldValue)->Unit,
    validate: (TextFieldValue)->Boolean = {true},
    modifier: Modifier = Modifier,
    label: String = "",
    textStyle:TextStyle = TextStyle(
        fontSize = 16.sp,
        fontWeight = FontWeight.Bold
    ),
    keyboardType: KeyboardType = KeyboardType.Text,
    color: Color = Colors.ChipGray
){
    val valid = validate(text)

    val color = if (valid) color else Colors.NotCompleted

    Stack(modifier.background(color, shape = RoundedCornerShape(50)), alignment = Alignment.Center) {

        if (text.text.isEmpty())
            Text(text = label)

        BaseTextField(
            value = text,
            onValueChange = onValueChange,
            textStyle = textStyle,
            modifier = Modifier.padding(start = 8.dp, end = 8.dp).fillMaxWidth(),
            keyboardType = keyboardType,
        )
    }
}

这里讨论过。但是我无法从中得到可行的解决方案。 Change Cursor Position and force SingleLine of TextField in Jetpack Compose

0 个答案:

没有答案