How to get rid of the underline in a Spannable String with a Clickable Object?

后端 未结 10 2012
陌清茗
陌清茗 2021-02-03 16:44

I have a Spannable Object with a Clickable Object set to it. When the Spannable String is displayed in the TextView it has bl

相关标签:
10条回答
  • 2021-02-03 17:08
    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
                        }
                    }
    
    0 讨论(0)
  • 2021-02-03 17:11

    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.

    0 讨论(0)
  • 2021-02-03 17:12

    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);
    
    0 讨论(0)
  • 2021-02-03 17:13

    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);
    
    0 讨论(0)
  • 2021-02-03 17:13

    Based on the answer of @Sai Gopi Me

    in Kotlin:

    val clickableSpan: ClickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            // some action here
        }
                    
        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            ds.isUnderlineText = false
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-03 17:20

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题