【问题标题】:String objects with various Hyperlinks in Textview?Textview中具有各种超链接的字符串对象?
【发布时间】:2022-01-08 12:18:32
【问题描述】:

我有一个 String 对象,其中包含我试图在 Textview 中单击的超链接,这是一个示例:

    String descrption = "\"Cardano is a decentralised platform" +
            " that will allow complex programmable transfers of" +
            " value in a secure and scalable fashion. It is one" +
            " of the first blockchains to be built in the highly" +
            " secure Haskell programming language. Cardano is developing" +
            " a <a href=\\\"https://www.coingecko.com/en?category_id=29\\\">smart contract platform</a>" +
            " which seeks to deliver more advanced features than any protocol previously developed." +
            " It is the first blockchain platform to evolve out of a scientific philosophy and a research-first driven approach." +
            " The development team consists of a large global collective of expert engineers and researchers.\\r\\n\\r\\n" +
            "The Cardano project is different from other blockchain projects as it openly addresses the need for regulatory oversight whilst maintaining" +
            " consumer privacy and protections through an innovative software architecture.";


    TextView textView = findViewById(R.id.description_textview);
    Linkify.addLinks(textView, Linkify.WEB_URLS);

但结果是这样的:

如何让超链接的格式正确?

【问题讨论】:

  • 这行得通,但它删除了所有换行符和回车符,所以我只得到一个长的连续文本。

标签: android hyperlink textview


【解决方案1】:

我已经设法让它使用以下代码:

descrption= descrption.replaceAll("\\r\\n", "<p>");
Spanned spanned= Html.fromHtml(descrption);
textView .setText(spanned);
textView.setMovementMethod(LinkMovementMethod.getInstance());

【讨论】:

  • &lt;br&gt; 将是比&lt;p&gt; 换行更好的选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2012-10-19
  • 2013-06-21
  • 2014-05-30
  • 2016-06-07
  • 2018-11-17
  • 1970-01-01
相关资源
最近更新 更多