Android TextWatcher在Emulator/Phone中的行为不同

Android TextWatcher在Emulator/Phone中的行为不同,android,textwatcher,Android,Textwatcher,我有一个像这样的编辑文本的文本观察者 // the text changed listener for the search field private TextWatcher searchWatcher = new TextWatcher() { @Override public void afterTextChanged(Editable span) { Log.v(TAG, "afterTextChanged: "+etSearch.getText().toStrin

我有一个像这样的编辑文本的文本观察者

// the text changed listener for the search field
private TextWatcher searchWatcher = new TextWatcher()
{

  @Override
  public void afterTextChanged(Editable span)
  {
    Log.v(TAG, "afterTextChanged: "+etSearch.getText().toString());
  }

  @Override
  public void beforeTextChanged(CharSequence s, 
                              int start, 
                              int count,
                              int after)
  {
    Log.v(TAG, "beforeTextChanged: "+etSearch.getText().toString()
      +"; start="+start+"; count="+count+"; after="+after);
  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before, int count)
  {
    Log.v(TAG, "onTextChanged: "+etSearch.getText().toString());
  }
}
(其中etSearch是我的编辑文本,带有
etSearch.addTextChangedListener(searchWatcher)


我有一个运行2.1-update1的索尼爱立信Xperia和一个运行2.1-update1的AVD。在emulator中,我单击EditText,使用软键盘键入abc,然后点击del按钮一次。在手机上,我触摸编辑文本,在软键盘上键入abc,然后按一下del。在电话里,我得到了这个:

beforeTextChanged: ; start=0; count=0; after=1
onTextChanged: a
afterTextChanged: a

beforeTextChanged: a; start=0; count=1; after=2
onTextChanged: ab
afterTextChanged: ab

beforeTextChanged: ab; start=0; count=2; after=3
onTextChanged: abc
afterTextChanged: abc

beforeTextChanged: abc; start=0; count=3; after=2
onTextChanged: ab
afterTextChanged: ab
beforeTextChanged: ; start=0; count=0; after=1
onTextChanged: a
afterTextChanged: a

beforeTextChanged: a; start=1; count=0; after=1
onTextChanged: ab
afterTextChanged: ab

beforeTextChanged: ab; start=2; count=0; after=1
onTextChanged: abc
afterTextChanged: abc

beforeTextChanged: abc; start=2; count=1; after=0
onTextChanged: ab
afterTextChanged: ab
在emulator上,我得到以下信息:

beforeTextChanged: ; start=0; count=0; after=1
onTextChanged: a
afterTextChanged: a

beforeTextChanged: a; start=0; count=1; after=2
onTextChanged: ab
afterTextChanged: ab

beforeTextChanged: ab; start=0; count=2; after=3
onTextChanged: abc
afterTextChanged: abc

beforeTextChanged: abc; start=0; count=3; after=2
onTextChanged: ab
afterTextChanged: ab
beforeTextChanged: ; start=0; count=0; after=1
onTextChanged: a
afterTextChanged: a

beforeTextChanged: a; start=1; count=0; after=1
onTextChanged: ab
afterTextChanged: ab

beforeTextChanged: ab; start=2; count=0; after=1
onTextChanged: abc
afterTextChanged: abc

beforeTextChanged: abc; start=2; count=1; after=0
onTextChanged: ab
afterTextChanged: ab

为什么它们不一样?哪一种是正确的行为?

您确定在这两种情况下执行的操作完全相同吗?虽然不同,但两种结果都有意义。然而,模拟器的结果看起来更符合逻辑。例如:

beforeTextChanged: ab; start=2; count=0; after=1 
对我说,在位置2(开始=2)处,您没有更多字符(计数=0),但又添加了1个字符(在=1之后)。这正是当您将字符串从
ab
扩展到
abc
时发生的情况

另一方面,Xperia说

beforeTextChanged: ab; start=0; count=2; after=3 
对我说,从位置0开始,您用3个新字符替换了2个现有字符。在这种情况下,您是否会从数据库中删除
ab
并添加
abc

更新:

根据对如何进行更改的最新描述,正确的behviour是在emulator上观察到的


在NexusOne上也观察到了在模拟器上观察到的相同行为。所以我想说,在Xperia上观察到的行为更像是异常。

在emulator中,我单击编辑文本,使用软键盘键入abc,然后点击del按钮一次。在手机上,我触摸编辑文本,在软键盘上键入abc,然后按一下del。(在原始帖子中添加了澄清。)re:update——这是我的想法,因为它与参考()中的TextWatcher匹配,所以我可以假设Xperia很奇怪,其他手机也能正确地完成它?@Ben Williams-在检查之前不想给出我的答案。我检查过了,在NexusOne上观察到的行为与您在emulator上观察到的行为相同。谢谢。我不明白为什么Xperia会这么做,但我最终还是设法在一个愿望上检查了它,这个愿望也像模拟器一样工作,所以我假设索尼很奇怪。@Ben Williams-HTC Hero也一样工作(正确),所以我同意你的看法。