我怎么可以做一些事情,0.5秒我的EditText后的文本改变了吗? [英] How can I do something, 0.5 second after text changed in my EditText?

查看:141
本文介绍了我怎么可以做一些事情,0.5秒我的EditText后的文本改变了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的过滤一个EditText我的名单。我想过滤列表中的用户后的 0.5,第二个有完成打字的EditText 。我使用了 TextWatcher afterTextChanged 事件作此用途。但是,这一事件上升为EditText上每个字符的变化。

我应该怎么办?

解决方案

  editText.addTextChangedListener(
    新TextWatcher(){
        @覆盖公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){}
        @覆盖公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,诠释之后){}

        私人定时器定时=新的Timer();
        私人最终长延时= 500; //毫秒

        @覆盖
        公共无效afterTextChanged(最终可编辑S){
            timer.cancel();
            定时器=新的Timer();
            timer.schedule(
                新的TimerTask(){
                    @覆盖
                    公共无效的run(){
                        // TODO:做什么,你需要在这里(刷新列表)
                        //你可能需要使用runOnUiThread(Runnable的行动)的一些具体行动
                    }
                },
                延迟
            );
        }
    }
);
 

诀窍是取消并重新安排定时器每一次,当的EditText 文本得到改变。祝你好运!

I am filtering my list using an EditText. I want to filter the list 0.5 second after user has finished typing in EditText. I used the afterTextChanged event of TextWatcher for this purpose. But this event rises for each character changes in EditText.

What should I do?

解决方案

editText.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) { }

        private Timer timer=new Timer();
        private final long DELAY = 500; // milliseconds

        @Override
        public void afterTextChanged(final Editable s) {
            timer.cancel();
            timer = new Timer();
            timer.schedule(
                new TimerTask() {
                    @Override
                    public void run() {
                        // TODO: do what you need here (refresh list)
                        // you will probably need to use runOnUiThread(Runnable action) for some specific actions
                    }
                }, 
                DELAY
            );
        }
    }
);

The trick is in canceling and re-scheduling Timer each time, when text in EditText gets changed. Good luck!

这篇关于我怎么可以做一些事情,0.5秒我的EditText后的文本改变了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆