http://www.dewen.org/q/1744
ndroid 自定义TextView超链接
2个答案
试试这两个方法
- 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
- 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());
- }
- }