How to make text underline and remove text at the same time

孤人 提交于 2019-12-24 18:10:38

问题


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

回答1:


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

EDIT:

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


来源:https://stackoverflow.com/questions/49026982/how-to-make-text-underline-and-remove-text-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!