我正在编写一个程序,用户必须在其中搜索我的一些数据。
我已经创建了一个供用户输入文本的编辑视图,但是我想在用户确认输入后运行一个方法。
我该怎么做?
谢谢。
答案 0 :(得分:1)
如果您要添加搜索功能,为什么不使用搜索视图呢?
答案 1 :(得分:0)
有很多选项可以做到这一点,我在下面写了3个选项。
1 - If you have confirm button then use its onclicklistener for you method call.
2 - If you don't have confirm button then add focusChangeListener in edittext and call you method when edittext focus is lost.
3 - You can use a timerTask like when user stopped typing for 2 second (we assume that user input is done) you can call your method
示例:
edt_search.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) {
// user is typing: reset already started timer (if existing)
if (timer != null) {
timer.cancel();
}
}
@Override
public void afterTextChanged(final Editable s) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// Search Logic
if (!s.toString().trim().isEmpty())
// Your Function call
}
}, 500); // 350ms delay before the timer executes the "run“ method from TimerTask
}
}