开发者

TextWatcher behaves differently in Emulator/Phone

开发者 https://www.devze.com 2023-03-08 12:05 出处:网络
I have a TextWatcher on an EditText like this // the text changed listener for the search field private TextWatcher searchWatcher = new TextWatcher()

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号