TextWatcher 在模拟器/手机中的行为有所不同

发布于 2024-11-09 10:04:23 字数 1862 浏览 0 评论 0原文

我在这样的 EditText 上有一个 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().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) 的 Edittext。)

我有一台运行 2.1-update1 的索尼爱立信 Xperia 和一个也运行 2.1-update1 的 AVD。在模拟器中,我单击 EditText,使用软键盘输入 abc,然后按一次 del 按钮。在电话上,我触摸 EditText,在软键盘上输入 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

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

I have a TextWatcher on an EditText like this

// 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());
  }
}

(where etSearch is my Edittext with etSearch.addTextChangedListener(searchWatcher).)

I have a Sony Ericsson Xperia running 2.1-update1 and an AVD also running 2.1-update1. In the emulator, I click the EditText, type in abc using the softkeyboard, and hit the del button once. On the phone, I touch the EditText, type abc on the softkeyboard and hit del once. On the phone, I get this:

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

On the emulator, I get this:

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

Why aren't they the same? Which one is the correct behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃素的狼 2024-11-16 10:04:23

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

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

更新:

根据对更改方式的更新描述,正确的行为是在模拟器上观察到的行为。

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

Are you sure that you are doing completely the same operations in both cases? Although different, both resutls can make sense. However, emulator result looks more logical. For example:

beforeTextChanged: ab; start=2; count=0; after=1 

says to me that at the position 2 (start = 2) you had no more characters (count = 0) but you added 1 more character (after = 1). And that is exactly what happened when you expanded your string from ab to abc.

On the other hand Xperia says

beforeTextChanged: ab; start=0; count=2; after=3 

says to me that starting from the position 0, you replaced 2 existing characters with 3 new. Could it be that you in this case deleted ab and added abc from the beggining?

UPDATE:

According to updated description of the way how the change was made, correct behviour is the one observed on the emulator.

The same behaviour as observed on the emulator is also observed on Nexus One. So I would say that behaviour obsereved on Xperia is more like exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文