我正在使用两个textview
来显示数据库中的链接,我设法更改了链接颜色,但是我想删除下划线
email.setText(c.getString(5));
website.setText(c.getString(6));
Linkify.addLinks(email, Linkify.ALL);
Linkify.addLinks(website, Linkify.ALL);
我可以从XML或代码中做到这一点吗?
我正在使用两个textview
来显示数据库中的链接,我设法更改了链接颜色,但是我想删除下划线
email.setText(c.getString(5));
website.setText(c.getString(6));
Linkify.addLinks(email, Linkify.ALL);
Linkify.addLinks(website, Linkify.ALL);
我可以从XML或代码中做到这一点吗?
Answers:
您可以通过查找URLSpan
实例并将实例替换为不带下划线的版本,从而在代码中做到这一点。调用之后Linkify.addLinks()
,stripUnderlines()
在每个上调用下面粘贴的函数TextView
:
private void stripUnderlines(TextView textView) {
Spannable s = new SpannableString(textView.getText());
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
s.setSpan(span, start, end, 0);
}
textView.setText(s);
}
这需要URLSpan的自定义版本,该版本不启用TextPaint的“下划线”属性:
private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
Spannable s = (Spannable) textView.getText()
与Spannable s = new SpannableString(textView.getText());
不适合我的工作。如果我切换回强制转换,则只有在TextView具有的情况下它才起作用android:textIsSelectable=true
。知道为什么吗?
android:autoLink
属性起作用?否则,您似乎必须为内置功能创建自己的onclick方法。
给定一个textView和内容:
TextView textView = (TextView) findViewById(R.id.your_text_view_id);
String content = "your <a href='http://some.url'>html</a> content";
这是从超链接中删除下划线的简洁方法:
Spannable s = (Spannable) Html.fromHtml(content);
for (URLSpan u: s.getSpans(0, s.length(), URLSpan.class)) {
s.setSpan(new UnderlineSpan() {
public void updateDrawState(TextPaint tp) {
tp.setUnderlineText(false);
}
}, s.getSpanStart(u), s.getSpanEnd(u), 0);
}
textView.setText(s);
这是基于robUx4建议的方法。
为了使链接可点击,您还需要调用:
textView.setMovementMethod(LinkMovementMethod.getInstance());
String content = getResources().getString(R.string.content);
包含链接的链接将不再起作用。
res/values/strings.xml
包含与示例完全相同的字符串时,它不显示URL 。可以测试一下吗?
>
= >
,<
= <
等。例如:<string name="link_to_google" ><a href="https://www.google.com/">Google</a></string>
请参阅developer.android.com/guide/topics/resources/string-resource
UnderlineSpan
已经存在,但只能设置下划线。
另一种解决方案是在每个现有上不加下划线URLSpan
。因此,下划线状态在绘画前即被禁用。这样,您可以将您的URLSpan
(可能是自定义的)类和所有其他样式保留在其他位置。
public class NoUnderlineSpan extends UnderlineSpan {
public NoUnderlineSpan() {}
public NoUnderlineSpan(Parcel src) {}
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}
这是在不删除现有URLSpan对象的情况下进行设置的方法:
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
NoUnderlineSpan noUnderline = new NoUnderlineSpan();
s.setSpan(noUnderline, start, end, 0);
}
我认为我已经实施了一个更优雅的解决方案。我订TextView
了风俗。这样,您无需为每个TextView
带有超链接的代码执行额外的代码。
package com.example.view;
import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.URLSpan;
import android.util.AttributeSet;
import com.example.utils.UrlSpanNoUnderline;
public class TextViewNoUnderline extends AppCompatTextView {
public TextViewNoUnderline(Context context) {
this(context, null);
}
public TextViewNoUnderline(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}
public TextViewNoUnderline(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setSpannableFactory(Factory.getInstance());
}
private static class Factory extends Spannable.Factory {
private final static Factory sInstance = new Factory();
public static Factory getInstance() {
return sInstance;
}
@Override
public Spannable newSpannable(CharSequence source) {
return new SpannableNoUnderline(source);
}
}
private static class SpannableNoUnderline extends SpannableString {
public SpannableNoUnderline(CharSequence source) {
super(source);
}
@Override
public void setSpan(Object what, int start, int end, int flags) {
if (what instanceof URLSpan) {
what = new UrlSpanNoUnderline((URLSpan) what);
}
super.setSpan(what, start, end, flags);
}
}
}
并为UrlSpanNoUnderline编写代码:
package com.jankstudios.smmagazine.utils;
import android.text.TextPaint;
import android.text.style.URLSpan;
public class UrlSpanNoUnderline extends URLSpan {
public UrlSpanNoUnderline(URLSpan src) {
super(src.getURL());
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
这是Kotlin扩展功能:
fun TextView.removeLinksUnderline() {
val spannable = SpannableString(text)
for (u in spannable.getSpans(0, spannable.length, URLSpan::class.java)) {
spannable.setSpan(object : URLSpan(u.url) {
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = false
}
}, spannable.getSpanStart(u), spannable.getSpanEnd(u), 0)
}
text = spannable
}
用法:
txtView.removeLinksUnderline()
每当我尝试仅使用以下内容时,尝试使用可扩展性删除URL下划线:
1> 创建自定义类:
private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
这需要URLSpan的自定义版本,该版本不启用TextPaint的“下划线”属性
2> setSpan和跨度文本:
spannableText.setSpan(new URLSpanNoUnderline(UrlText), 0, UrlText.length() , 0);
在这里,spannableText是对象SpannableString ... !!!
如果您正在使用Textview自动链接属性,并且想要删除下划线,则可以使用它:
首先,扩展UnderlineSpan并删除下划线:
public class NoUnderlineSpan extends UnderlineSpan {
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}
其次,创建NoUnderlineSpan并实例化,从String文本创建Spannable并将span设置为spannable:
NoUnderlineSpan mNoUnderlineSpan = new NoUnderline();
if (yourTextView.getText() instanceof Spannable) {
Spannable s = (Spannable) yourTextView.getText();
s.setSpan(mNoUnderlineSpan, 0, s.length(), Spanned.SPAN_MARK_MARK);
}
参考:http : //prog3.com/sbdm/blog/maosidiaoxian/article/details/39156563
如果您只想输入文字而不必担心URL链接
这将删除链接,但保留文本
private Spannable stripLinks(String content) {
Spannable s = new SpannableString(content);
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span : spans) {
s.removeSpan(span);
}
return s;
}
无需其他课程
String content = "<a href='http://stackoverflow.com'>Stack Overflow</a> Rocks!";
textView.setText(stripLinks(content));
这是我的方法
public static void removeUnderlines(Spannable p_Text) {
if (p_Text != null && p_Text.toString().length() > 0) {
URLSpan[] spans = p_Text.getSpans(0, p_Text.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = p_Text.getSpanStart(span);
int end = p_Text.getSpanEnd(span);
p_Text.removeSpan(span);
span = new URLSpanNoUnderline(span.getURL());
p_Text.setSpan(span, start, end, 0);
}
}
}
像这样称呼它
AppController.removeUnderlines((Spannable) eventEmail.getText());
Appcontroller是我的应用程序类,我在其中放置了此方法,以便可以从任何地方访问它
对于Xamarin用户找到这篇文章,这是我如何使其工作的方式:
class URLSpanNoUnderline : URLSpan {
public URLSpanNoUnderline (string url) : base (url) {
}
public override void UpdateDrawState (TextPaint ds) {
base.UpdateDrawState (ds);
ds.UnderlineText = false;
}
}
public static void StripUnderlinesFromLinks (this TextView textView) {
var spannable = new SpannableStringBuilder (textView.TextFormatted);
var spans = spannable.GetSpans (0, spannable.Length (), Java.Lang.Class.FromType (typeof (URLSpan)));
foreach (URLSpan span in spans) {
var start = spannable.GetSpanStart (span);
var end = spannable.GetSpanEnd (span);
spannable.RemoveSpan(span);
var newSpan = new URLSpanNoUnderline (span.URL);
spannable.SetSpan(newSpan, start, end, 0);
}
textView.TextFormatted = spannable;
}
public void setView()
{
TextView t=(TextView) findViewById(R.id.textView3);
t.setText(Html.fromHtml("<a href=http://www.urdusms.net > UrduSMS "));
t.setTextColor(Color.BLACK);
t.setGravity(Gravity.CENTER);
t.setMovementMethod(LinkMovementMethod.getInstance());
Spannable s = (Spannable) t.getText();
URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
for (URLSpan span: spans) {
int start = s.getSpanStart(span);
int end = s.getSpanEnd(span);
s.removeSpan(span);
span = new URLSpanline_none(span.getURL());
s.setSpan(span, start, end, 0);
}
t.setText(s);
}
//inner class is
private class URLSpanline_none extends URLSpan {
public URLSpanline_none(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}
tel:2125551212
。new URLSpanNoUnderline
应该传递该呼叫span.getURL()
以保留此信息,否则,您将生成错误的链接,这些链接会在单击时导致异常。由于我自己没有编辑权限,因此已将这个建议的解决方案放入您的答案的编辑队列中。