我有一个Edit text,用来观察自动更新搜索页面的变化。每输入一个字符,观察器就会被启动,所以对于一个搜索查询,如 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.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET.NET:
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.
}
你需要为这个用例使用一个处理程序。你没有处理以前的查询结果,你必须丢弃以前的结果并发出一个新的查询。
private var runnable: Runnable? = null
private var handler: Handler? = null
private var timeToWait = 2000L //change this one for delay (time in milli)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
setListenersForSearch()
}
private fun setListenersForSearch() {
//Initialize your handler
handler = Handler()
etSearch.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
runnable = Runnable {
//check if it is not empty then search
if (etSearch.text.toString().isEmpty().not()) {
search(etSearch.text.toString())
}
}
handler?.postDelayed(runnable, timeToWait)
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
//remove call back for old query
handler?.removeCallbacks(runnable)
}
})
}
private fun search(query: String) {
//hit your API call
}