gpt4 book ai didi

android - 不一致的 onTextChanged() 行为

转载 作者:行者123 更新时间:2023-12-04 17:07:57 27 4
gpt4 key购买 nike

我写了下面的代码来弄清楚 onTextChanged() 方法是如何工作的:

override fun onTextChanged(string: CharSequence?, start: Int, before: Int, count: Int) {
Log.d(TAG, "onTextChanged triggered.")
Log.d(TAG, "string = $string")
Log.d(TAG, "start = $start")
Log.d(TAG, "before = $before")
Log.d(TAG, "count = $count")
Log.d(TAG, "____________________________________________________________")

这是我注意到的:当我只输入字母时,计数非常简单:

onTextChanged triggered.
string = a
start = 0
before = 0
count = 1
____________________________________________________________
onTextChanged triggered.
string = aa
start = 0
before = 1
count = 2
____________________________________________________________
onTextChanged triggered.
string = aaa
start = 0
before = 2
count = 3
____________________________________________________________
onTextChanged triggered.
string = aaaa
start = 0
before = 3
count = 4
____________________________________________________________
onTextChanged triggered.
string = aaaaa
start = 0
before = 4
count = 5
____________________________________________________________

现在,如果我添加 3 个(或更多)数字,然后继续输入字母,则会发生以下情况:

onTextChanged triggered.
string = aaaaa2
start = 0
before = 5
count = 6
____________________________________________________________
onTextChanged triggered.
string = aaaaa22
start = 0
before = 6
count = 7
____________________________________________________________
onTextChanged triggered.
string = aaaaa222
start = 0
before = 7
count = 8
____________________________________________________________
onTextChanged triggered.
string = aaaaa222
start = 0
before = **8** <---- This is when it gets funny
count = **8**
____________________________________________________________
onTextChanged triggered.
string = aaaaa222a
start = 8
before = 0
count = 1
____________________________________________________________
onTextChanged triggered.
string = aaaaa222aa
start = 9
before = 0
count = 1
____________________________________________________________
onTextChanged triggered.
string = aaaaa222aaa
start = 10
before = 0
count = 1
____________________________________________________________
onTextChanged triggered.
string = aaaaa222aaaa
start = 11
before = 0
count = 1
____________________________________________________________
onTextChanged triggered.
string = aaaaa222aaaaa
start = 12
before = 0
count = 1
____________________________________________________________

对我来说,似乎输入 3 位数字会以某种方式改变 TextWatcher 的行为,使其将后面的所有内容视为 Char 而不是 CharSequence。

是否有对此行为的解释?

最佳答案

虽然我没有解释为什么它会发生在 3 位数字上,但我发现根本无法保证偏移量何时会失效。检查这个this

“你不会被告知更改发生在哪里,因为其他 afterTextChanged() 方法可能已经进行了其他更改并且使偏移量无效。但是如果你需要知道这里,你可以使用 Spannable#setSpanonTextChanged(CharSequence, int, int, int) 中标记您的位置,然后从此处查找跨度结束的位置。”

TextWatcher 的一个可能实现是 this one .这样您将拥有正确的开始和结束索引。

关于android - 不一致的 onTextChanged() 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70116446/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com