我喜歡上一個EditText一個TextWatcher這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是我EDITTEXT與etSearch.addTextChangedListener(searchWatcher)
。)
我有一個索尼愛立信Xperia運行2.1 UPDATE1並且AVD也運行2.1-update1。在模擬器中,我點擊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
他們爲什麼不一樣呢?哪一個是正確的行爲?
在模擬器中,我單擊EditText,使用軟鍵盤輸入abc,然後單擊del按鈕一次。在手機上,我觸摸EditText,在軟鍵盤上輸入abc並敲擊del一次。 (由澄清原來的職位。) – 2011-05-24 13:11:59
回覆:更新 - 這就是我想,是因爲它在參考(http://developer.android.com/reference/android/text/TextWatcher.html)匹配TextWatcher,所以我可以認爲,Xperia只是奇怪,其他手機會正確嗎? – 2011-05-24 13:28:08
@Ben Williams - 在檢查之前不想給我答案。我對它進行了檢查,在Nexus One上觀察到的行爲與您在仿真器上觀察到的行爲相同。 – Zelimir 2011-05-24 13:41:11