即使在添加观察程序之前设置了文本,也会调用 TextWatcher

发布于 2024-10-19 10:24:36 字数 1093 浏览 0 评论 0原文

在 Android 活动中,我首先恢复 EditText 中的文本,然后向其中添加 TextWatcher。

private static int WC = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TextWatcherTest", "onCreate:\t" +CLASS_NAME);
setContentView(R.layout.main);

EditText et = (EditText)findViewById(R.id.editText);
Log.e("TextWatcherTest", "Set text xyz");
et.setText("xyz");

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
    }
});
}

但是当我运行该活动时,即使在设置文本后添加了观察者本身,也会调用 afterTextChanged 方法。因此日志输出类似于

onCreate:    LifecycleMain
Set text xyz
// screen rotation
onCreate:    LifecycleMain
Set text xyz
afterTextChanged:    xyz    2

TextWatcher 中的计数器,显示被调用的观察程序是在 EditText 中设置文本后添加的观察程序。有什么想法为什么会发生这种情况以及如何防止这种情况发生?

in an android activity, i first recover the text in an EditText and add then a TextWatcher to it.

private static int WC = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("TextWatcherTest", "onCreate:\t" +CLASS_NAME);
setContentView(R.layout.main);

EditText et = (EditText)findViewById(R.id.editText);
Log.e("TextWatcherTest", "Set text xyz");
et.setText("xyz");

et.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
    @Override
    public void afterTextChanged(Editable s) {
        Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
    }
});
}

but when i run the activity, the afterTextChanged method is called even if the Watcher itself is added after setting the text. so the log output is something like

onCreate:    LifecycleMain
Set text xyz
// screen rotation
onCreate:    LifecycleMain
Set text xyz
afterTextChanged:    xyz    2

the counter in the TextWatcher shows that the watcher that is called is the one that was added AFTER the text was set in the EditText. any ideas why that happens and how i can prevent that?

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

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

发布评论

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

评论(2

桜花祭 2024-10-26 10:24:36

解决方案是将 addTextChangedListener 移至 onPostCreate 方法。一切都会得到解决。

Solution is to move your addTextChangedListener to onPostCreate method. all will be solved.

时光与爱终年不遇 2024-10-26 10:24:36

这肯定会发生。当savedinstanceState不为空时(意味着您之前的对象状态已保存),您正在设置文本。

我认为发生这种情况是因为您在 onCreate 方法中将 TextWatcher 添加到了 EditText 中,并且下次调用 onCreate 时(例如在配置更改后),然后它发现 TextWatcher 已经添加到了 EditText 中。

如果您想检查这种情况,请将其放在您的条件之前:

if(savedInstanceState == null){
    Log.e("savedInstance state is null", "no text");
    et.setText("No text");
}

在这种情况下,如果您在 EditText 上调用 setText,则不会调用 afterTextChanged(Editable s) 。

This definitely must be happening. You are setting text when savedinstanceState is not null (meaning your previous object states are saved).

I think that this happens because you added the TextWatcher to your EditText in the onCreate method, and next time onCreate gets called (e.g. after a configuration change) then it finds that the TextWatcher has already been added to the EditText.

If you want to check for this situation, put this before your condition:

if(savedInstanceState == null){
    Log.e("savedInstance state is null", "no text");
    et.setText("No text");
}

In this case, if you call setText on your EditText, then afterTextChanged(Editable s) will not be called.

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