0

当我没有选择时,我可以将文本附加到文本字段值,但是如果我进行选择并尝试附加到它,则会引发偏移量超出范围错误。

我目前的实现是:

fun TextFieldValue.add(): TextFieldValue {
return copy(
        annotatedString =
        annotatedString.subSequence(0, selection.min) +
                buildAnnotatedString { append("**") } +
                getSelectedText() +
                annotatedString.subSequence(selection.max, text.length),
        selection = TextRange(
            selection.min + 2, // length of the added string
            selection.max + 2
        ),
        composition = null
    )
}

我的理解是我从函数返回的值与 TextField 的先前值进行比较,因此出现越界错误。

一个合乎逻辑的前进方式是按顺序执行这些步骤,即:

  1. 更改字符串值
  2. 更改选择
  3. 改变构图

任何帮助,将不胜感激!

4

0 回答 0