如何摆脱带有可点击对象的Spannable字符串中的下划线?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何摆脱带有可点击对象的Spannable字符串中的下划线?相关的知识,希望对你有一定的参考价值。

我有一个Spannable ObjectClickable Object设置它。当Spannable String显示在TextView中时,它具有蓝色文本和蓝色下划线(向用户指示此文本是可点击的)。我的问题是如何防止在TextView中出现蓝色下划线?

答案

使用以下代码并尝试

String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  

class MyClickableSpan extends ClickableSpan{// extend ClickableSpan     

    String clicked;
    public MyClickableSpan(String string) {
        super();
        clicked = string;
    }

    public void onClick(View tv) {
       Toast.makeText(MainActivity.this,clicked , Toast.LENGTH_SHORT).show();
    }

    public void updateDrawState(TextPaint ds) {// override updateDrawState
        ds.setUnderlineText(false); // set to false to remove underline
    }
}
另一答案

这适合我。无需创建自定义ClickableSpan类。只需覆盖updateDrawState(TextPaint ds)

SpannableString span = new SpannableString("Some text");
ClickableSpan clickSpan = new ClickableSpan() {
    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(ds.linkColor);    // you can use custom color
        ds.setUnderlineText(false);    // this remove the underline
    }

    @Override
    public void onClick(View textView) {
        // handle click event
    }
};

span.setSpan(clickSpan, 5, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(span);
另一答案

覆盖ClickableSpan类的updateDrawState方法

String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  

class MyClickableSpan extends ClickableSpan{// extend ClickableSpan     

String clicked;
public MyClickableSpan(String string) {
    // TODO Auto-generated constructor stub
super();
clicked =string;
}

public void onClick(View tv) {

   Toast.makeText(MainActivity.this,clicked ,
        Toast.LENGTH_SHORT).show();
}

public void updateDrawState(TextPaint ds) {// override updateDrawState
   ds.setUnderlineText(false); // set to false to remove underline
}

用于更改spannable String的颜色

  SpannableString    ss = new SpannableString("android Stack Overflow");

  ForegroundColorSpan fcs=newForegroundColorSpan(Color.parseColor("#01579B"));
  ss.setSpan(fcs, 8,13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
另一答案

Raghunandan的回答非常适合我。这是它的简化版本:

public abstract class NoUnderlineClickableSpan extends ClickableSpan {    
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}
另一答案

spannableStringObject.toString();

编辑

SpannableString ss = getYourSpannableString();
UnderlineSpan[] uspans = ss.getSpans(0, ss.length(), UnderlineSpan.class);
for (UnderlineSpan us : uspans) {
    ss.removeSpan(us);
}

将从Spannable中删除所有UnderlineSpans。

另一答案
Try the below code to remove underlined and clicked event on multiple words in textview :



            String str="Angelina Clapped For Lester Kang";
            Spannable span = Spannable.Factory.getInstance().newSpannable(str);

            // 0 to 8 start and  index of Angelina
            span.setSpan(new ClickableSpan(str), 0, 8, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

             //  21 to 32  start and  index of Lester  Kang
            span.setSpan(new ClickableSpan(str), 21, 32, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            textview.setText(span);

             class ClickableSpan extends ClickableSpan{

                    String clicked;
                    public ClickableSpan (String string) {
                        super();

                    }

                    public void onClick(View v) {
                        Toast.makeText(MyActivity.this,""+((TextView)v).getText().toString(),Toast.LENGTH_SHORT).show();
                    }

                    public void updateDrawState(TextPaint ds) {
                       // override updateDrawState
                        ds.setUnderlineText(false); // set to false to remove underline
                    }
                }
另一答案

最简单的方法是

 string1 = new SpannableString("By Tapping Register You Agree To The 
Terms And Conditions");
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_SHORT).show();
        }
        @Override
        public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);    // this line removes underline
        }

    };
    text_terms.setMovementMethod(LinkMovementMethod.getInstance());
    string1.setSpan(clickableSpan,37,string1.length(),0);
    text_terms.setText(string1);

以上是关于如何摆脱带有可点击对象的Spannable字符串中的下划线?的主要内容,如果未能解决你的问题,请参考以下文章

什么是Objective-C中Android的“Spannable”等价物

为啥我的标志和导航栏之间有巨大的可点击间隙以及如何摆脱它?

有没有关于 Spanned 和 Spannable 文本的例子

Python 。如何摆脱字符串中的'\ r'?

带有可点击对象的jQuery可拖动列表 - 防止点击拖动

android分享带有指示谷歌地图的可点击字符串url