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

后端 未结 10 2013
陌清茗
陌清茗 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:23

    ANURAG RASTOGI's answer saved the day for me! I already have a formatted SpannableString on which I wanted to apply a ClickableSpan:

    spannableString.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            // Do something...
        }
        // Prevent
        // ds.setColor(ds.linkColor);
        // ds.setUnderlineText(true);
        // in: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/ClickableSpan.java
        // from executing.
        @Override
        public void updateDrawState(@NonNull TextPaint ds) {
            // super.updateDrawState(ds);
        }
    },0, spannableString.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
    

    The updateDrawState overrides the updateDrawState in the ClickableSpan class in Android, and by not calling super.updateDrawState it will not get executed.

    All text formatting already present in spannableString will be preserved.

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

    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;
        }
        @Override
        public void onClick(View tv) {
           Toast.makeText(MainActivity.this,clicked , Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void updateDrawState(TextPaint ds) {// override updateDrawState
            ds.setUnderlineText(false); // set to false to remove underline
        }
    }
    
    0 讨论(0)
  • 2021-02-03 17:26

    If you do not want any pre-applied attributes do not call super.updateDrawState(). On overriding updateDrawState(object:Textpaint) with help of object you can apply or call different functions present in Text Paint.

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

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