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("<", "<").replaceAll(">",">");
    value = value.replaceAll("\n", "
"); 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/

你可能感兴趣的:(android)