如果用户在EditText中暂停键入一段时间后如何触发操作?
答案 0 :(得分:7)
你可以这样做:
final Handler mHandler = new Handler();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(userStoppedTyping, 2000); // 2 second
}
Runnable userStoppedTyping = new Runnable() {
@Override
public void run() {
// user didn't typed for 2 seconds, do whatever you want
}
};
});