公告
财富商城
积分规则
提问
发文
2020-01-24 06:08发布
虎瘦雄心在
I want to catch an event when the user finishes editing EditText.
How can it be done?
I had the same problem when trying to implement 'now typing' on chat app. try to extend EditText as follows:
public class TypingEditText extends EditText implements TextWatcher { private static final int TypingInterval = 2000; public interface OnTypingChanged { public void onTyping(EditText view, boolean isTyping); } private OnTypingChanged t; private Handler handler; { handler = new Handler(); } public TypingEditText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.addTextChangedListener(this); } public TypingEditText(Context context, AttributeSet attrs) { super(context, attrs); this.addTextChangedListener(this); } public TypingEditText(Context context) { super(context); this.addTextChangedListener(this); } public void setOnTypingChanged(OnTypingChanged t) { this.t = t; } @Override public void afterTextChanged(Editable s) { if(t != null){ t.onTyping(this, true); handler.removeCallbacks(notifier); handler.postDelayed(notifier, TypingInterval); } } private Runnable notifier = new Runnable() { @Override public void run() { if(t != null) t.onTyping(TypingEditText.this, false); } }; @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { }
}
最多设置5个标签!
I had the same problem when trying to implement 'now typing' on chat app. try to extend EditText as follows:
}