我有Spannable Object
一个Clickable Object
集来。在Spannable String
中显示时,TextView
它具有蓝色文本和蓝色下划线(向用户指示此文本是可单击的)。我的问题是如何防止出现蓝色下划线TextView
?
Answers:
使用以下代码并尝试
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
}
}
这对我有用。无需创建自定义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);
覆盖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);
最简单的方法是
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);
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
}
}
spannableStringObject.toString();
编辑
SpannableString ss = getYourSpannableString();
UnderlineSpan[] uspans = ss.getSpans(0, ss.length(), UnderlineSpan.class);
for (UnderlineSpan us : uspans) {
ss.removeSpan(us);
}
将从Spannable中删除所有UnderlineSpans。
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
覆盖updateDrawState
在ClickableSpan
Android中类,并没有要求super.updateDrawState
它不会得到执行。
已经存在的所有文本格式spannableString
将被保留。
如果您不希望任何预先应用的属性,请不要调用super.updateDrawState()。在覆盖updateDrawState(对象:Textpaint)与帮助对象可以申请或拨打出现在文本画图不同的功能。
基于@ 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
}
}
}
ds.setColor(Color.rgb(0, 0 , 255));
在可扩展字符串上保持蓝色(或将其更改为所需的任何颜色)。