如何使段落标签内的锚标签可点击?

时间:2019-05-09 07:21:09

标签: java android html parsing

下面是我的字符串

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);

1 个答案:

答案 0 :(得分:0)

如果您在XML文件的autoLink中添加了linksClickableTextView属性,请删除它。

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

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));
}

所以现在您的布局将是:

enter image description here

当您单击“职业页面”时,它将在Web浏览器中打开关联的URL。