/FIXED\ Android - TextView.setText 字符串有 \n 问题

/FIXED\ Android - TextView.setText string with \n problem

我有一个方法可以从 Firebase 中检索一些文本:

    db = FirebaseFirestore.getInstance();
    db.collection("Contact").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                for (DocumentSnapshot document : task.getResult()) {
                    String textt = (document.getString("Text"));
                    settext(textt);
                }
            }
        }
    });

当我检索字符串 "Text" 时,我调用方法 settext :

private void settext(String textt){
    mainTextView.setText(textt);
}

这是字符串 textt 的值:"azerty123\nqwerty\ntest" 但是 mainTextView 打印 "azerty123\nqwerty\ntest" 没有换行符。

有人可以帮我吗?

已修复,只需将方法 settext 重写为:

private void settext(String textt){
    String text = textt.replaceAll("\\n", "\n");
    mainTextView.setText(text);
}

在里面试试这个 onComplete():

String textt = document.getString("Text").replaceAll("\\n", "\n");

String textt = document.getString("Text").replace("\n", "\n");

以防来自数据库的字符串包含特殊字符。