android - Android EditText观察者受API调用时间限制?

标签 android kotlin textview textwatcher

我有一个“编辑”文本,用于观察自动更新搜索页面的更改。在输入的每个字符上,都会触发观察程序,因此对于apple这样的搜索查询,该查询会触发为

D/debug: a
D/debug: ap
D/debug: app
D/debug: appl
D/debug: apple

所以我用计时器添加了2s的延迟,日志看起来像
D/debug: apple
D/debug: apple
D/debug: apple
D/debug: apple
D/debug: apple

2s之后立即将它们一起触发。这是我的文字观察器,
search_et.addTextChangedListener( object: TextWatcher{
    override fun afterTextChanged(s: Editable?) {
        val timer = Timer()
        timer.schedule(object : TimerTask() {
            override fun run() {
                Log.d("debug", "$s");
            }
        }, 2000)            
    }
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
})

我要完成的任务是在2秒内从EditText获取一次值,在这种情况下我该怎么办才能正确处理?

最佳答案

使用处理程序概念,我们可以这样做。通过这种方式,我们可以限制号码。在编辑文本中实现搜索功能时的api调用数量。

Java:

        private Handler textSearchHandler  = new Handler(); //declare it as a global variable
        et_FilterStopsFromListActivity_FilterETxt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textSearchHandler.removeCallbacksAndMessages(null); //It will clear all previous callbacks
            }

            @Override
            public void afterTextChanged(final Editable s) {
                textSearchHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       callApi();//do whatever you want to do
                        }
                    }
                }, 2000);

            }
        });

Kotlin :
private var textChangedHandler = Handler() // declare it globally.
tie_password.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(editable: Editable?) {
            textChangedHandler.postDelayed(runnable,2000)
        }

        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            textChangedHandler.removeCallbacksAndMessages(runnable)
        }
    })

 var runnable = Runnable {
    callApi() //do whatever you want to do here.
}

关于android - Android EditText观察者受API调用时间限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61968225/

相关文章:

android - BottomSheetDialogFragment 的圆角不适用于 API <21

Android 重用 Activity 堆栈中的 Activity 并清除它

amazon-web-services - AWS S3 java SDK : Can't create bucket

java - 如何在可点击的 TextView 中设置超链接?安卓Java

android - 根据文本区域的宽度计算文本大小

android - 有没有办法为 SpannableString 设置文本阴影?

android - 获取 StreamCorruptedException

java - 如何使布局占屏幕宽度的 80%?

android - Android 中的 Kotlin 协程 : Why use bg() from Anko instead of async()?

android-studio - 解决 Android Studio 中克隆的 Kotlin 应用程序缺少运行配置的问题