83

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

4

10 回答 10

149

使用下面的代码并尝试

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
    }
}
于 2013-04-15T05:50:15.113 回答
64

这对我有用。无需创建自定义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);
于 2016-08-16T13:11:32.660 回答
8

Raghunandan 的回答对我来说非常有效。这是它的精简版:

public abstract class NoUnderlineClickableSpan extends ClickableSpan {    
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}
于 2015-05-30T01:30:02.147 回答
5

覆盖 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
}

用于更改可跨字符串的颜色

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

  ForegroundColorSpan fcs=newForegroundColorSpan(Color.parseColor("#01579B"));
  ss.setSpan(fcs, 8,13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
于 2015-03-30T13:42:44.863 回答
3

最简单的方法是

 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);
于 2017-10-18T05:52:46.797 回答
1

spannableStringObject.toString();

编辑

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

将从 Spannable 中删除所有 UnderlineSpans。

于 2013-04-15T03:26:34.477 回答
1

基于@Sai Gopi Me的回答

在科特林:

val clickableSpan: ClickableSpan = object : ClickableSpan() {
    override fun onClick(widget: View) {
        // some action here
    }
                
    override fun updateDrawState(ds: TextPaint) {
        super.updateDrawState(ds)
        ds.isUnderlineText = false
        }
    }
}
于 2020-11-16T03:27:33.513 回答
1
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
                    }
                }
于 2016-02-05T09:32:29.950 回答
1

ANURAG RASTOGI 的回答为我节省了一天!我已经有一个SpannableString我想应用的格式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);

updateDrawState覆盖Android 类updateDrawState中的ClickableSpan,并且不调用super.updateDrawState它不会被执行。

所有已经存在的文本格式spannableString都将被保留。

于 2020-06-29T15:02:13.060 回答
0

如果您不想要任何预先应用的属性,请不要调用super.updateDrawState()。在 object 的帮助下覆盖 updateDrawState(object:Textpaint) 时,可以应用或调用 Text Paint 中存在的不同函数。

于 2020-05-15T15:52:04.783 回答