How to detect if users stop typing in EditText android(如何检测用户是否停止在 EditText android 中输入)
问题描述
我的布局中有一个 EditText 字段.当用户停止在该编辑文本字段中输入时,我想执行一项操作.我已经实现了 TextWatcher 并使用了它的功能
I have an EditText field in my layout. I want to perform an action when the user stops typing in that edittext field. I have implemented TextWatcher and use its functions
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { }
@Override
public void afterTextChanged(Editable editable) {
}
函数 onTextChanged
和 afterTextChanged
在输入任何字符后被调用,但我想在用户完成输入该编辑文本字段后执行操作,就像 facebook在它的签入"页面上.
The function onTextChanged
and afterTextChanged
get called just after typing any character, but I want to perform action after the user has finished typing in that edittext field, just like facebook does on it's "Check In" page.
我该如何实现?
推荐答案
这就是我的工作方式!
long delay = 1000; // 1 seconds after user stops typing
long last_text_edit = 0;
Handler handler = new Handler();
private Runnable input_finish_checker = new Runnable() {
public void run() {
if (System.currentTimeMillis() > (last_text_edit + delay - 500)) {
// TODO: do what you need here
// ............
// ............
DoStuff();
}
}
};
EditText editText = (EditText) findViewById(R.id.editTextStopId);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged (CharSequence s,int start, int count,
int after){
}
@Override
public void onTextChanged ( final CharSequence s, int start, int before,
int count){
//You need to remove this to run only once
handler.removeCallbacks(input_finish_checker);
}
@Override
public void afterTextChanged ( final Editable s){
//avoid triggering event when text is empty
if (s.length() > 0) {
last_text_edit = System.currentTimeMillis();
handler.postDelayed(input_finish_checker, delay);
} else {
}
}
}
);
这篇关于如何检测用户是否停止在 EditText android 中输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测用户是否停止在 EditText android 中输入
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01