Android-Android 自定义TextView超链接
目标效果
- 例如微博,点击@Xxx时打开用户首页,而#Xxxx#是打开话题页面
- 但是,要保留系统默认的style(默认下划线+蓝色字体),而且长按时,可以复制
- 如果是邮件超链接,如何在打开邮件应用时,预先输入部分内容/模板
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
package com.zhou.activity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.text.style.URLSpan;
import android.widget.TextView;
public class TextViewLinkActivity extends Activity {
TextView myTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTextView = (TextView) this.findViewById(R.id.myTextView);
//创建一个 SpannableString对象
SpannableString sp = new SpannableString("这句话中有链接,有高亮显示,这样,或者这样,还有斜体.");
//设置超链接
sp.setSpan(new URLSpan("http://www.abc.com"), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置高亮样式一
sp.setSpan(new BackgroundColorSpan(Color.RED), 17 ,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//设置高亮样式二
sp.setSpan(new ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//设置斜体
sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27, 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
//SpannableString对象设置给TextView
myTextView.setText(sp);
设置TextView可点击
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
试试这两个方法
TextView location = (TextView) findViewById(R.id.location);
location.setMovementMethod(LinkMovementMethod.getInstance());
Spannable spans = (Spannable) location.getText();
ClickableSpan clickSpan = new ClickableSpan() {
@Override
public void onClick(View widget)
{
//put whatever you like here, below is an example
AlertDialog.Builder builder = new Builder(MainActivity.this);
builder.setTitle("Location clicked");
AlertDialog dialog = builder.create();
dialog.show();
}
};
spans.setSpan(clickSpan, 0, spans.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href="http://www.google.com">link</a> " +
"created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());
TextView textView = (TextView) findViewById(R.id.TextBox);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(strText));
//e-mail的字符串内容
<a href="mailto:my@email.com">my@email.com</a>
去掉下划线的方法
email.setText(c.getString(5));
website.setText(c.getString(6));
Linkify.addLinks(email, Linkify.ALL);
Linkify.addLinks(website, Linkify.ALL);
private void stripUnderlines(TextView textView) {
Spannable s = (Spannable)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
private class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}