如何使文本下划线并同时删除文本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使文本下划线并同时删除文本相关的知识,希望对你有一定的参考价值。

我有一个文本,其中有指示标记,指示从哪里我将文本下划线,我想从该指标使文本下划线也想删除指标,以便它不会出现在字符串中,这是我正在尝试:

  String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
    int i1 = consent.indexOf(">");
    int i2 = consent.indexOf("</");
    consentCheck.setMovementMethod(LinkMovementMethod.getInstance());

    consentCheck.setText(consent, CheckBox.BufferType.SPANNABLE);

    consent = consent.replace("</clickable>", "");
    consent = consent.replace("<clickable>", "");

    Spannable mySpannable = (Spannable)consentCheck.getText();
    mySpannable.setSpan(clickableSpan, i1+1, i2 , Spannable.SPAN_INCLUSIVE_INCLUSIVE);
答案

如果要在TextView中显示文本,可以用“u”标签替换“可点击”标签,然后在setText中使用html.fromHtml():

String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
consent = consent.replace("<clickable>", "<u>").replace("</clickable>", "</u>");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    tvConsent.setText(Html.fromHtml(consent, Html.FROM_HTML_MODE_LEGACY));
} else {
    tvConsent.setText(Html.fromHtml(consent));
}

编辑:

如果您想自由编辑“同意”部分,您可以将文本分成三个部分,然后单独编辑每个部分

String consent = "By clicking this, you confirm you understand the services provided by your Health at Hand doctor and give <clickable>consent</clickable> to proceed.";
String start = "<clickable>";
String end = "</clickable>";
String part1 = consent.substring(0, consent.indexOf(start));
String part2 = consent.substring(consent.indexOf(start)+start.length(),consent.indexOf(end));
String part3 = consent.substring(consent.indexOf(end), consent.length()).replace(end, "");

以上是关于如何使文本下划线并同时删除文本的主要内容,如果未能解决你的问题,请参考以下文章

粘贴到 KendoUI 编辑器时,如何从文本中删除所有 html 格式?

如何使标签文本下划线?

使底部边框更接近文本

如何从 Sqlplus 输出中删除下划线

如何在android中为pdf查看器制作注释,如突出显示、删除线、下划线、绘制、添加文本等?

将图像上传到 Firebase 存储并同时将文本输入到 Firestore 时出错