代码之家  ›  专栏  ›  技术社区  ›  stuck

如何仅在一行中动态更改textview

  •  0
  • stuck  · 技术社区  · 7 年前

    正如你在 this 图像,我的坐标正在动态更改 \n .我希望它只显示一行中的最后一个坐标,只是动态地更改,我该怎么做?

    这是我的代码:

    tv_loc.append("Lattitude: " + lattitude + "  Longitude: " + longitude + "\n");
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Sagar    7 年前

    如果您使用 append() 然后每次你调用它时,它都会附加一个新行。因为您的目的是更新同一行,所以请使用以下命令

    tv_loc.setText("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");

    而不是

    tv_loc.append("Lattitude: " + lattitude + " Longitude: " + longitude + "\n");

        2
  •  0
  •   thomaz andrade    7 年前

    您需要更新文本,而不是附加文本。

    当您附加文本时,它将添加到字符串的末尾。假设您有以下代码:

    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);

    (The \n 不需要)