【问题标题】:newline ("\n") and tab ("\t") doesn't work when displaying String in TextView?在 TextView 中显示字符串时,换行符(“\n”)和制表符(“\t”)不起作用?
【发布时间】:2016-09-17 06:53:56
【问题描述】:

字符串来自 API 响应,当尝试在 TextView\n\t 上显示它时不起作用。这是示例字符串:

经济舱\n以这种票价类型购买的预订:\n\n-\允许在预定起飞时间前 48 小时进行航班改签,但需按每个航段每位乘客收取改签费,外加任何适用的票价差额。\n-\不允许更改名称。\n-\运力受到控制和限制,因此可能无法在所有航班上提供。\n-\始终受我们的一般条款和运输条件和费用表(如适用)。 \n

【问题讨论】:

  • 如何显示?请解释更多,最好显示一些代码
  • 您的文本在 textView 中硬编码时可以正常工作。给我看看你的代码。
  • 字符串显示与上面给出的相同,我想将 \n 应用于新行并将 \t 应用于空格,但那些特殊字符显示在字符串中
  • 你如何显示这个?在什么小部件上? TextViewEditText 等?
  • 来自 API 响应的字符串,非硬编码

标签: android string textview


【解决方案1】:
  String pureString = android.text.Html.fromHtml(fareRules).toString();
    String str1 = pureString.replace("[", "");
    str1 = str1.replace("]", "");
    str1 = str1.replace("\"", "");
    str1 = str1.replace("-", "");
    str1 = str1.replace("\\n", System.getProperty("line.separator"));
    str1 = str1.replace("\\t", getString(R.string.tab));
    ((TextView) dialog.findViewById(R.id.fare_rules)).setText(str1);

【讨论】:

    【解决方案2】:

    试试这个。

    string = string.replace("\\\n", System.getProperty("line.separator"));
    

    【讨论】:

    • 它不起作用,\n 的包含被删除但输出没有进入新行
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    相关资源
    最近更新 更多