0

我正在开发一个聊天应用程序。当用户输入或用户完全删除时,我可以获得更新输入状态的事件,我可以更新为“未输入状态”并显示为在线。直到这个过程正常工作。

但问题是当用户输入一些行并停止时,我不应该显示在 whatsapp 中应用的输入。如何处理?

这是我所做的代码。

        ChatMsg.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                   if (edtChatMsg.getText().toString().trim().length() > 0) {
                    if (!isTyping) {
                            isTyping = true;
                            serviceCall();
                          }
                  else{
                      isTyping = false;
                        serviceCall();
                      }
            }
        so at result
    @Override
     protected void onTyping(String message) {
    if (message.equalsIgnoreCase("Typing…")) {
        txtUserPersonStatus.setText("Typing…");
    } else {
        txtUserPersonStatus.setText("Online");
    }
}

我的问题是当用户输入键盘一段时间然后停止时如何处理。

谢谢。

4

3 回答 3

5

基本上你需要实现某种超时。每次用户键入内容时,您都必须安排超时并重置您之前安排的任何超时。因此,当用户停止键入时,计时器会在指定时间后触发。

您可以使用Handler以下示例执行此操作:

final int TYPING_TIMEOUT = 5000; // 5 seconds timeout
final Handler timeoutHandler = new Handler();
final Runnable typingTimeout = new Runnable() {
    public void run() {
        isTyping = false;
        serviceCall();
    }
};

ChatMsg.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // reset the timeout
        timeoutHandler.removeCallbacks(typingTimeout);

        if (edtChatMsg.getText().toString().trim().length() > 0) {
            // schedule the timeout
            timeoutHandler.postDelayed(typingTimeout, TYPING_TIMEOUT);

            if (!isTyping) {
                isTyping = true;
                serviceCall();
            }
        }
        else {
            isTyping = false;
            serviceCall();
        }
    }
});
于 2015-12-24T15:17:51.150 回答
0

使用处理程序:

_AFKHandler = new Handler();
_AFKRunnable = new Runnable() {
            public void run() {
                isTyping = false;
                        serviceCall();
            }
        };

ChatMsg.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                   _AFKHandler.removeCallbacks(_AFKRunnable);
                   if (edtChatMsg.getText().toString().trim().length() > 0) {
                    if (!isTyping) {
                            isTyping = true;
                            serviceCall();
                            _AFKHandler.postDelayed(_AFKRunnable,AFK_TIMEOUT_VALUE_IN_MS);
                          }
                  else{
                      isTyping = false;
                        serviceCall();
                      }
            }
        so at result
    @Override
     protected void onTyping(String message) {
    if (message.equalsIgnoreCase("Typing…")) {
        txtUserPersonStatus.setText("Typing…");
    } else {
        txtUserPersonStatus.setText("Online");
    }
}
于 2015-12-24T14:48:22.410 回答
0

我认为你需要使用:

@Override
public void afterTextChanged(Editable arg0) {
    //  arg0=null ,Handler ---is empty ;

}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // Handler --- is typing


}
于 2015-12-24T16:11:29.130 回答