【问题标题】:How to make a anchor tag clickable which is inside paragraph tag?如何使段落标签内的锚标签可点击?
【发布时间】:2019-09-26 23:29:12
【问题描述】:

下面是我的字符串

String txtLink = "<p>Apply directly in our <a 
href=\"https://theorem.applytojob.com/apply/TyJy6YCsOs/Experienced- 
Backend-Engineer-Ruby?source=GitHub+Jobs\">careers page</a></p>";

我需要在 Android 中可以点击锚标记。到目前为止我尝试过的是

txtJobLink.setText(""+Html.fromHtml(txtLink));
txtJobLink.setMovementMethod(BetterLinkMovementMethod.newInstance());
BetterLinkMovementMethod.linkify(Linkify.ALL,txtJobLink);
BetterLinkMovementMethod.linkifyHtml(txtJobLink);
txtJobLink.setLinksClickable(true);

【问题讨论】:

  • 尝试将链接放在一个撇号内。
  • 它来自 API

标签: java android html parsing


【解决方案1】:

如果您在 xml 文件中的 TextView 中添加了 autoLinklinksClickable 属性,请将其删除。

并更改您的活动代码,例如:

String txtLink = "<p>Apply directly in our <a href=\"https://theorem.applytojob.com/apply/TyJy6YCsOs/Experienced-Backend-Engineer-Ruby?source=GitHub+Jobs\">careers page</a></p>";

TextView txtJobLink = findViewById(R.id.txtJobLink);
txtJobLink.setMovementMethod(LinkMovementMethod.getInstance());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    txtJobLink.setText(Html.fromHtml(txtLink, Html.FROM_HTML_MODE_LEGACY));
} else {
    txtJobLink.setText(Html.fromHtml(txtLink));
}

所以现在你的布局会是这样的:

当你点击“职业页面”时,它会在网络浏览器中打开相关的网址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2023-03-03
    • 1970-01-01
    • 2016-05-17
    • 2021-04-02
    相关资源
    最近更新 更多