简体   繁体   English

控制Android版TextWatcher中的事件触发

[英]Control firing of events in TextWatcher for Android

I had to recreate the functionality of AutoCompleteTextViews with EditTexts (for different reasons* & that's probably another question, but i'm down that path now). 我不得不用EditTexts重新创建AutoCompleteTextViews的功能(出于不同的原因*,这可能是另一个问题,但是我现在沿着这条路走)。

I've got all my functionality working by issuing my api calls whenever the Text on my EditText changes. 每当EditText上的Text更改时,我都会通过发出api调用来实现所有功能。 I do this by using TextWatcher (overriding the afterTextChanged method). 我通过使用TextWatcher(重写afterTextChanged方法)来做到这一点。

Is there a way i can control, the way the calls are fired with the listener? 有什么我可以控制的方法,可以通过监听器触发电话吗?

Here's my use case: 这是我的用例:

I'm searching for California . 我在寻找加利福尼亚

I check for a min. 我检查一分钟。 length of 3 and then start firing calls. 长度为3,然后开始触发通话。 so number of API calls currently sent = 8 因此,当前发送的API调用数= 8

  1. Cal 卡尔
  2. Cali 卡利
  3. Calif 加利福尼亚州
  4. Califo
  5. Califor Califor
  6. Californ Californ
  7. Californi 加利福尼亚州
  8. California 加州

Is there a way I can cancel off fired events, if i receive a new text change? 如果收到新的文字更改,是否可以取消已触发的事件? So when the user types California, i wait until there's a gap where no new text is entered, and then fire a call to my API? 因此,当用户输入“加利福尼亚”时,我会等到没有输入新文本的间隙,然后再调用我的API?

It doesn't have to be very accurate. 它不一定非常准确。 An additional event fire here and there is completely acceptable. 此处发生的其他事件完全可以接受。 I'm looking to reduce the number of events fired from 8 to atleast 3. 我希望将触发的事件数量从8个减少到至少3个。

I wanted to show the autocomplete results in a different listview vs. the pop-up window that comes alongside the auto-complete textview. 我想在不同的列表视图中显示自动完成结果,而不是在自动完成文本视图旁边显示弹出窗口。

Try following TextWatcher: 尝试遵循TextWatcher:

TextWatcher t = new TextWatcher() {
        long lastChange = 0;

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {

            // TODO Auto-generated method stub
            if (s.length() > 3) {
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        if (System.currentTimeMillis() - lastChange >= 300) {
                            //send request
                        }
                    }
                }, 300);
                lastChange = System.currentTimeMillis();

            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM