本文介紹了如何檢測用戶是否停止在 EditText android 中輸入的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我的布局中有一個 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 中輸入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!