简体   繁体   English

如何在聊天应用程序中以粗体设置新收到的消息

[英]How can I set a newly received message in bold in a chatapp

I am trying to build a simple chat app with a tutorial I followed.我正在尝试使用我遵循的教程构建一个简单的聊天应用程序。 I would like to implement some code that when you view all your current chat partners and somebody sends you a new message, that that message style goes into bold until I read it and after that goes back to normal.我想实现一些代码,当您查看所有当前聊天伙伴并且有人向您发送新消息时,该消息样式将变为粗体,直到我阅读它,然后恢复正常。 Now I only know you can change the style in the xml file.现在我只知道你可以更改xml文件中的样式。 But by doing that it changes for all the messages.但是通过这样做,它会更改所有消息。 I want it only for a new incoming unread message.我只想要一条新的未读消息。 Any advice?有什么建议吗?

What I have now: https://imgur.com/a/sUeMTK2我现在拥有的: https : //imgur.com/a/sUeMTK2

What I want: https://imgur.com/a/3YkBDzI我想要什么: https : //imgur.com/a/3YkBDzI

//check for last message
private void lastMessage(final String userid, final TextView last_msg){
    theLastMessage = "default";
    final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");

    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Chat chat = snapshot.getValue(Chat.class);
               if (firebaseUser != null && chat != null) {
                    if (chat.getReceiver().equals(firebaseUser.getUid()) && chat.getSender().equals(userid) ||
                            chat.getReceiver().equals(userid) && chat.getSender().equals(firebaseUser.getUid())) {
                        theLastMessage = chat.getMessage();
                    }
                }
            }

            switch (theLastMessage){
                case  "default":
                    last_msg.setText("No Message");
                    break;

                default:
                    last_msg.setText(theLastMessage);
                    break;
            }

            theLastMessage = "default";
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

//XML FILE:
<TextView
    android:id="@+id/last_msg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/username"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="5dp"
    android:layout_toRightOf="@id/profile_image"
    android:maxLines="1"
    android:paddingTop="5dp"
    android:textStyle="bold"
    android:textColor="@color/colorPrimaryDark" />

You can do it with HTML using html tag for bold:您可以使用 html 标记为粗体使用 HTML 来做到这一点:

String bold= "<b>" + theLastMessage + "</b> ";
 last_message.setText(Html.fromHtml(bold));
 if (!chat.isIsseen() && firebaseUser.getUid().equals(chat.getReceiver())){

                       last_msg.setTypeface(null, Typeface.BOLD_ITALIC );

                    }else {

                        last_msg.setTypeface(null,Typeface.ITALIC);

                    }

Add this after this在这之后添加这个

theLastMessage = chat.getMessage();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Android Beam确认收到的消息? - How can I confirm my message was received using Android Beam? 如何在 Java 中设置异常消息? - How can I set the message on an exception in Java? 如何设置匹配的消息? - How can I set a matching message? 如何在POI Apache Java中同时设置单元格fontHeight 9和Bold - How can i set cell fontHeight 9 and Bold concurrently in poi apache java 如何设置新创建的Java File对象的内容类型? - How can I set the content type of a newly created Java File object? 如何使用Java从该公司收到的短信中提取跟踪ID和快递公司名称? - How can I extract tracking ID and Courier name from text message received from that company using java? 开始我的第一个Android应用程序 如何轻松测试服务器是否收到消息? - Starting my first android app. How can I easily test if the server has received a message? 每次发送或接收消息时,如何使最后联系的用户出现在 recyclerview 的列表顶部? - How can I make last contacted user appear on top of list in recyclerview each time a message is sent or received? 如何将收到服务器的消息转发到Netty(Java)中的另一台服务器? - How can I forward a message that received a server to another server in Netty (Java)? 如何在“加粗”之前设置文本 ” - how to set text in Bold Before “ | ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM