在聊天应用程序中如何使用 Firebase 实现打字指示器

Ars*_*lan -1 java android firebase firebase-realtime-database

在聊天应用程序中,如何使用 Firebase 在 Android Studio 中实现某人正在打字的打字指示符,就像 whats-app 或 Messenger 一样

如图

Ale*_*amo 5

为此,您需要在 Firebase 数据库中添加一个名为:的新字段,typing默认值为false。然后使用addTextChangedListener()你的方法EditText来实际查看某人何时键入消息。当有人输入某些东西时,onTextChanged()方法就会被触发。现在将 的值typing从 false更改为true。在此之后,addValueEventListener查看值何时更改。如果值为 true,则在聊天室中显示该消息。因此,我建议您使用以下代码:

yourEditText.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s)) {
            //Set the value of typing field to true.
        } else {
            // Set to false
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void afterTextChanged(Editable s) {}
});
Run Code Online (Sandbox Code Playgroud)