Skip to content

Commit d63ab3f

Browse files
committed
优化数据展示到textView
1 parent 1f427b1 commit d63ab3f

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

base_iotutils/src/main/java/com/face_chtj/base_iotutils/FormatViewUtils.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,29 @@ public static void scrollBackToTop(TextView textView) {
3535
textView.setText ("");
3636
}
3737

38+
public static void formatData(TextView tv, String str, String pattern){
39+
formatData(tv, str, pattern,false);
40+
}
41+
3842
/**
3943
* show data to Activity
4044
*
4145
* @param str Support html tags
4246
* @param pattern time format yyyyMMddHHmmss or ....
4347
*/
44-
public static void formatData(TextView tv, String str, String pattern) {
48+
public static void formatData(TextView tv, String str, String pattern,boolean jumpFirstLine) {
4549
// 假设 ObjectUtils.isEmpty 和 TimeUtils.getTodayDateHms 已经定义
4650
if (tv != null && !ObjectUtils.isEmpty(str)) {
4751

48-
// --- 换行处理的核心修改 ---
49-
// 1. 将字符串中的普通换行符 '\n' 替换为 HTML 换行标签 "<br>"。
50-
String htmlStr = str.replace("\n", "<br>");
52+
// --- 换行处理的核心优化 ---
53+
// 1. 处理 Windows/DOS 换行:将 "\r\n" 替换为 "<br>"
54+
String htmlStr = str.replace("\r\n", "<br>");
55+
56+
// 2. 处理 Unix/Linux/Android 换行:将单个 "\n" 替换为 "<br>"
57+
htmlStr = htmlStr.replace("\n", "<br>");
58+
59+
// 3. (可选) 处理旧 Mac 换行:将单个 "\r" 替换为 "<br>"
60+
htmlStr = htmlStr.replace("\r", "<br>");
5161
// ------------------------
5262

5363
// 如果行数大于 MAXIMUM_ROW ,清空内容
@@ -69,8 +79,12 @@ public static void formatData(TextView tv, String str, String pattern) {
6979
// 滚动逻辑 (保持不变)
7080
Layout layout = tv.getLayout();
7181
if (layout != null) {
72-
int scrollAmount = layout.getLineTop(tv.getLineCount()) - tv.getHeight();
73-
tv.scrollTo(0, scrollAmount > 0 ? scrollAmount : 0);
82+
if (jumpFirstLine){
83+
tv.scrollTo(0, 0);
84+
}else{
85+
int scrollAmount = layout.getLineTop(tv.getLineCount()) - tv.getHeight();
86+
tv.scrollTo(0, scrollAmount > 0 ? scrollAmount : 0);
87+
}
7488
}
7589
}
7690
}
@@ -81,7 +95,7 @@ public static void formatData(TextView tv, String str, String pattern) {
8195
* @param htmlStr Support html tags
8296
*/
8397
public static void formatData(TextView tv, String htmlStr) {
84-
formatData (tv, htmlStr, "");
98+
formatData (tv, htmlStr, "",false);
8599
}
86100

87101
public static String formatUnderline(int color, String content) {

0 commit comments

Comments
 (0)