[英]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.