How to get rid of the underline in a Spannable Str

2019-01-26 02:54发布

I have a Spannable Object with a Clickable Object set to it. When the Spannable String is displayed in the TextView it has blue text and a blue underline (indicating to the user that this Text is Clickable). My problem is how can I prevent appearing the blue underline in TextView?

7条回答
狗以群分
2楼-- · 2019-01-26 03:13

Use the below code and try

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
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-26 03:13

Override updateDrawState method of ClickableSpan class

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
}

For changing color of spannable String

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

  ForegroundColorSpan fcs=newForegroundColorSpan(Color.parseColor("#01579B"));
  ss.setSpan(fcs, 8,13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
查看更多
虎瘦雄心在
4楼-- · 2019-01-26 03:15

Raghunandan's answer works perfectly for me. Here is a pared-down version of it:

public abstract class NoUnderlineClickableSpan extends ClickableSpan {    
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}
查看更多
smile是对你的礼貌
5楼-- · 2019-01-26 03:20

spannableStringObject.toString();

Edit

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

Will remove all the UnderlineSpans from the Spannable.

查看更多
放荡不羁爱自由
6楼-- · 2019-01-26 03:21

simplest way is

 string1 = new SpannableString("By Tapping Register You Agree To The \nTerms 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);
查看更多
小情绪 Triste *
7楼-- · 2019-01-26 03:33

This works for me. No need to create custom ClickableSpan class. Just override 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);
查看更多
登录 后发表回答