如何知道,从addTextChangedListener,如何文本已更改本已、addTextChangedListener、何文

2023-09-04 04:48:19 作者:玛丽莲萌兔

我有一个EditText和一个ListView的屏幕。当您键入的EditText,一个AsyncTask的启动(任何现有的AsyncTask被取消,并抛出)查询当地的一些数据在ListView中显示。如果是的EditText空文本,所有的数据查询并显示出来。本质上,的EditText是屏幕上的数据的过滤器。这是很容易做到。在addTextChangedListener,只需取消previous的AsyncTask,推出一个新的,并传递给它s.toString()。

I have a screen with an EditText and a ListView. As you type into the EditText, an AsyncTask is launched (any existing AsyncTask is cancelled and tossed) to query locally for some data to display in the ListView. If the EditText is empty of text, all data is queried for and displayed. Essentially, the EditText is a filter of the data on the screen. This is easy to do. In the addTextChangedListener, simply cancel your previous AsyncTask, launch a new one, and pass to it s.toString().

当用户点击退格删除的EditText上一些文字,我想默认的行为(其中所有数据填充的列表)。这适用于我目前的addTextChangedListener的执行情况。

When the user hits the backspace to erase some text in the EditText, I want the default behavior (where all data populates the list). This works with my current implementation of addTextChangedListener.

不过,我也有在右边我的EditText的X按钮。这是为了清除掉的EditText( EditText.setText(); )。这是一种特殊情况。在这种情况下,我不希望查询的情况发生。问题是,addTextChangedListener似乎并不足够聪明的知道这一点。它被触发,因为文本已发生变化,所有数据中查询。

However, I also have an X button on the right side of my EditText. This is intended to clear out the EditText (EditText.setText("");). This is a special case. In this case, I do not want the query to happen. The problem is, addTextChangedListener does not seem intelligent enough to know this. It gets triggered because the text has changed and all data is queried for.

我如何prevent呢?

How do I prevent this?

推荐答案

使用布尔ignoreNextTextChangeKThxBye 数据成员,设置/清除了布尔酌情使用如果()语句,以避免做查询。

Use a boolean ignoreNextTextChangeKThxBye data member, setting/clearing that boolean as appropriate and using an if() statement to avoid doing the query.