如何在 Android 中查找 EditText 的输入状态?
How to find typing status of EditText in Android?
我想在用户开始输入或停止输入时查找 EditText 的输入状态。我找到了 addTextChangedListener
但它不断触发事件。任何帮助将非常感激。您可以从任何聊天应用程序中参考打字状态。
P.S:除了addTextChangedListener
,请给我建议
像这样为您的editText定义addTextChangeListener,这非常适合获取写入回调。
public void afterTextChanged(Editable s) {
if (!TextUtils.isEmpty(s.toString()) && s.toString().trim().length() == 1) {
updateIsTyping("y");
isType = true;
} else if (s.toString().trim().length() == 0 && false) {
updateIsTyping("n");
isType = false;
}
}
如果我没理解错的话,您想在用户开始输入和停止输入时触发一个事件。 addTextChangeListener 是解决方案的一部分,但您应该将其与计时器和布尔值结合使用。
每次用户开始输入时,将布尔值设置为 true 并记录时间。定期检查时间——如果自用户上次输入以来已经过了几秒左右,则将其视为用户已停止输入,将布尔值设置为 false 并重置计时器。
我已经通过创建自定义 EditText
public class CustomTypingEditText extends CustomEditTextNormal implements TextWatcher {
private static final int TypingInterval = 800;
//your listener interface that you implement anonymously from the Activity
public interface OnTypingModified {
void onIsTypingModified(EditText view, boolean isTyping);
}
private OnTypingModified typingChangedListener;
//members you would need for the small thread that declares that you have stopped typing
private boolean currentTypingState = false;
private Handler handler = new Handler();
private Runnable stoppedTypingNotifier = new Runnable() {
@Override
public void run() {
//part A of the magic...
if (null != typingChangedListener) {
typingChangedListener.onIsTypingModified(CustomTypingEditText.this, false);
currentTypingState = false;
}
}
};
public CustomTypingEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.addTextChangedListener(this);
}
public CustomTypingEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.addTextChangedListener(this);
}
public CustomTypingEditText(Context context) {
super(context);
this.addTextChangedListener(this);
}
public void setOnTypingModified(OnTypingModified typingChangedListener) {
this.typingChangedListener = typingChangedListener;
}
@Override
public void afterTextChanged(Editable s) {
//part B of the magic...
if (null != typingChangedListener) {
if (!currentTypingState) {
typingChangedListener.onIsTypingModified(this, true);
currentTypingState = true;
}
handler.removeCallbacks(stoppedTypingNotifier);
handler.postDelayed(stoppedTypingNotifier, TypingInterval);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int after) {
}
}
这里是听众,
etMessage.setOnTypingModified(new CustomTypingEditText.OnTypingModified() {
@Override
public void onIsTypingModified(EditText view, boolean isTyping) {
if(isTyping){
Log.i(TAG, "onIsTypingModified: User started typing.");
}else{
Log.i(TAG, "onIsTypingModified: User stopped typing");
}
}
});
我想在用户开始输入或停止输入时查找 EditText 的输入状态。我找到了 addTextChangedListener
但它不断触发事件。任何帮助将非常感激。您可以从任何聊天应用程序中参考打字状态。
P.S:除了addTextChangedListener
像这样为您的editText定义addTextChangeListener,这非常适合获取写入回调。
public void afterTextChanged(Editable s) {
if (!TextUtils.isEmpty(s.toString()) && s.toString().trim().length() == 1) {
updateIsTyping("y");
isType = true;
} else if (s.toString().trim().length() == 0 && false) {
updateIsTyping("n");
isType = false;
}
}
如果我没理解错的话,您想在用户开始输入和停止输入时触发一个事件。 addTextChangeListener 是解决方案的一部分,但您应该将其与计时器和布尔值结合使用。
每次用户开始输入时,将布尔值设置为 true 并记录时间。定期检查时间——如果自用户上次输入以来已经过了几秒左右,则将其视为用户已停止输入,将布尔值设置为 false 并重置计时器。
我已经通过创建自定义 EditText
public class CustomTypingEditText extends CustomEditTextNormal implements TextWatcher {
private static final int TypingInterval = 800;
//your listener interface that you implement anonymously from the Activity
public interface OnTypingModified {
void onIsTypingModified(EditText view, boolean isTyping);
}
private OnTypingModified typingChangedListener;
//members you would need for the small thread that declares that you have stopped typing
private boolean currentTypingState = false;
private Handler handler = new Handler();
private Runnable stoppedTypingNotifier = new Runnable() {
@Override
public void run() {
//part A of the magic...
if (null != typingChangedListener) {
typingChangedListener.onIsTypingModified(CustomTypingEditText.this, false);
currentTypingState = false;
}
}
};
public CustomTypingEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.addTextChangedListener(this);
}
public CustomTypingEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.addTextChangedListener(this);
}
public CustomTypingEditText(Context context) {
super(context);
this.addTextChangedListener(this);
}
public void setOnTypingModified(OnTypingModified typingChangedListener) {
this.typingChangedListener = typingChangedListener;
}
@Override
public void afterTextChanged(Editable s) {
//part B of the magic...
if (null != typingChangedListener) {
if (!currentTypingState) {
typingChangedListener.onIsTypingModified(this, true);
currentTypingState = true;
}
handler.removeCallbacks(stoppedTypingNotifier);
handler.postDelayed(stoppedTypingNotifier, TypingInterval);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence text, int start, int before, int after) {
}
}
这里是听众,
etMessage.setOnTypingModified(new CustomTypingEditText.OnTypingModified() {
@Override
public void onIsTypingModified(EditText view, boolean isTyping) {
if(isTyping){
Log.i(TAG, "onIsTypingModified: User started typing.");
}else{
Log.i(TAG, "onIsTypingModified: User stopped typing");
}
}
});