我如何仅在一行中动态更改文本视图
How do i change dynamically textview in one single row only
正如您在 this 图像中看到的,我的坐标正在动态变化 \n
。我希望它只显示一行中的最后一个坐标,只是动态变化,我该怎么做?
这是我的代码:
tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");
如果您使用 append()
那么每次调用它时,它都会附加一个新行。由于您的目的是更新同一行,因此请使用以下
tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");
而不是
tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");
您需要更新文本而不是附加文本。
当您追加文本时,它会添加到字符串的末尾。假设您有此代码:
String tv_loc = "";
For(int i = 0; i < 10; i++) {
tv_loc.append(i + " ");
}
system.out.println(tv_loc);
它将打印 0 1 2 3 4 5 6 7 8 9
,因为您正在 附加 它。
要解决此问题,您需要 更新 文本,为此您可以使用 setText()
函数(假设 tv_loc
是一个 TextView 对象), 喜欢
tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude);
(不需要\n
)
正如您在 this 图像中看到的,我的坐标正在动态变化 \n
。我希望它只显示一行中的最后一个坐标,只是动态变化,我该怎么做?
这是我的代码:
tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");
如果您使用 append()
那么每次调用它时,它都会附加一个新行。由于您的目的是更新同一行,因此请使用以下
tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");
而不是
tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");
您需要更新文本而不是附加文本。
当您追加文本时,它会添加到字符串的末尾。假设您有此代码:
String tv_loc = "";
For(int i = 0; i < 10; i++) {
tv_loc.append(i + " ");
}
system.out.println(tv_loc);
它将打印 0 1 2 3 4 5 6 7 8 9
,因为您正在 附加 它。
要解决此问题,您需要 更新 文本,为此您可以使用 setText()
函数(假设 tv_loc
是一个 TextView 对象), 喜欢
tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude);
(不需要\n
)