如何使文本加下划线并同时删除文本 [英] How to make text underline and remove text at the same time

查看:182
本文介绍了如何使文本加下划线并同时删除文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本,其中有一个指示符标记,该指示符指示我将从何处对文本添加下划线,我想从该指示符使文本添加下划线,也希望删除指示符,以使其不会出现在字符串中,这就是我要执行的操作我正在尝试:

I have a text in which there is indicator tag that indicate from where i will make text underline, I want to make text underline from that indicator also want to remove indicator so that it wont appear in string, here is what I'm trying:

  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中显示文本,可以将"clickable"标签替换为"u"标签,然后在setText中使用Html.fromHtml():

If you want to display the text in TextView you could replace "clickable" tags with "u" tags and then use Html.fromHtml() in setText:

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

如果您要自由编辑同意"部分,则可以将文本分为三部分,然后独立编辑每个部分

If u want to edit "consent" part freely you could split the text into three parts and then edit each part independently

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

这篇关于如何使文本加下划线并同时删除文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆