问题描述
||
我在这样的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 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
为什么不一样?正确的行为是哪一个?
解决方法
您确定在两种情况下都执行完全相同的操作吗?尽管有所不同,但两种结果都有意义。但是,仿真器结果看起来更合乎逻辑。例如:
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观察到的行为更像是例外。