android String与HTML的相互转换

1、保存TextView时将String值保存成HTML

public static String parseStringToHtml(String value)
  {
    if (TextUtils.isEmpty(value))
      {
        return value;
      }
    //注意"&"一定要放在第一个,要不然会把后面的<给去掉&
    value = value.replaceAll("&", "&").replaceAll(" "," ");//&\空格
    value = value.replaceAll("\"", """).replaceAll("'","'");//双引号、单引号
    value = value.replaceAll("/", "'").replaceAll("\"", """);
    value = value.replaceAll("<", "&lt;").replaceAll(">","&gt;");
    value = value.replaceAll("\n", "<br/>");
    return value;
  }

2、显示时,需要将String换HTML显示出来

String content = workRecordBean.getWork_connect();
if (!TextUtils.isEmpty(content))
  {
    workContentView.setText(Html.fromHtml(content));
  } else
  {
    workContentView.setText(content);
  }

 

参考资料:

https://tool.lu/htmlentity/


版权声明:本文为blog_yue原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。