afterTextChanged()回调被称为无文本被实际改变 [英] afterTextChanged() callback being called without the text being actually changed
问题描述
我有一个片段的EditText
和里面的 onCreateView()
我添加了一个 TextWatcher
到的EditText
。
I have a fragment with an EditText
and inside the onCreateView()
I add a TextWatcher
to the EditText
.
每个被加入第二次的片段时间 afterTextChanged(编辑S)
回调被调用而没有被修改的文本。
Each time the fragment is being added for the second time afterTextChanged(Editable s)
callback is being called without the text ever being changed.
下面是一个code片断:
Here is a code snippet :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
myEditText = (EditText) v.findViewById(R.id.edit_text);
myEditText.addTextChangedListener(textWatcher);
...
}
TextWatcher textWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
searchProgressBar.setVisibility(View.INVISIBLE);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "after text changed");
}
}
我还设置了片段保留其状态,我把片段的实例在活动。
I also set the fragment to retain its state, and I keep the instance of the fragment in the activity.
推荐答案
编辑解决方案:
,因为它似乎文本从片段连接,因为该片段恢复的意见previous状态,第二次改变。
As it seems the text was changed from the second time the fragment was attached because the fragment restored the previous state of the views.
我的解决办法是添加在文本观察家
的 onResume()
自从恢复了状态的前的 onResume
被调用。
My solution was adding the text watcher
in the onResume()
since the state was restored before the onResume
was called.
@Override
public void onResume() {
super.onResume();
myEditText.addTextChangedListener(textWatcher);
}
这篇关于afterTextChanged()回调被称为无文本被实际改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!