onTextChanged()行为不一致



我写了下面的代码来了解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位数字,但我发现根本无法保证offesets何时会无效。检查这个

"您没有被告知更改发生在哪里,因为其他afterTextChanged((方法可能已经进行了其他更改,并且使偏移量无效。但如果你需要知道这里,你可以在onTextChanged(CharSequence, int, int, int)中使用Spannable#setSpan来标记你的位置,然后从这里向上看跨度的终点">

CCD_ 3的一个可能的实现是这个。这样,您将拥有正确的开始索引和结束索引。

相关内容

  • 没有找到相关文章

最新更新