¿Cómo deshacerse del subrayado en una cadena Spannable con un Objeto Clickable?


Tengo un Spannable Object con un Clickable Object establecido en él. Cuando el Spannable String se muestra en el TextView tiene texto azul y un subrayado azul (indicando al usuario que este Texto es Clickable). Mi problema es cómo puedo evitar que aparezca el subrayado azul en TextView?

Author: Masoud, 2013-04-15

7 answers

Usa el siguiente código y prueba

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
    }
}
 80
Author: Raghunandan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-04 13:09:20

Esto funciona para mí. No es necesario crear una clase personalizada ClickableSpan. Simplemente anule 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);
 29
Author: ARiF,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-16 13:11:32

Anular el método updateDrawState de la clase ClickableSpan

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
}

Para cambiar el color de la cadena spannable

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

  ForegroundColorSpan fcs=newForegroundColorSpan(Color.parseColor("#01579B"));
  ss.setSpan(fcs, 8,13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
 4
Author: Sanjay Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-30 13:42:44

La respuesta de Raghunandan funciona perfectamente para mí. Aquí hay una versión reducida de la misma:

public abstract class NoUnderlineClickableSpan extends ClickableSpan {    
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}
 4
Author: Joseph Johnson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-30 01:30:02

spannableStringObject.toString();

Editar

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

Eliminará todos los UnderlineSpans del Spannable.

 2
Author: dcow,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-08-18 07:26:05
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
                    }
                }
 1
Author: Pradeep Gupta,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-05 09:32:29

La forma más simple es

 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);
 1
Author: saigopi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-18 05:52:46