Android Development and Implementation of TextView Hyperlink 5 Ways Source Code Example
- 2021-11-29 08:22:18
- OfStack
There are five ways for Android to implement TextView hyperlink 1: the fourth and fifth are recommended
1. Configure autoLink properties directly in the xml file (easy to use, single effect 1)
autoLink attribute 1 has six values, which are none (normal), web (recognizing text as a website address), phone (recognizing text as a telephone number), mail (recognizing text as an email address), map (this, uh, how to express it? The map application will open), and all (automatically recognizing according to text). Under normal circumstances, we can set it to all. Let's take a look. At this time, it will automatically identify the telephone number, email address and webpage link in TextView, which is the simplest one. Such as:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="
android:textSize="16dp" />
2. Use the HTML language
We know that TextView can directly display the converted HTML, so with the help of H5 development experience, we know that hyperlinks in web pages can also be opened in TextView, as follows:
As long as we write the agreement, this is actually very simple.
tv1.setText(Html.fromHtml("<a href='tel:18565554482'> Make a phone call </a>,<a href='smsto:18565554482'> Send a text message </a>,<a href='mailto:584991843@qq.com'> Send an email </a>,<a href='http://www.baidu.com'>Go Baidu </a>"));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
3. Write HTML directly in strings. xml, and then reference it directly in xml of TextView (similar to the second method)
The definitions in strings. xml are as follows:
<string name="tv4"><a href='tel:18565554482'> Make a phone call </a>,<a href='smsto:18565554482'> Send a text message </a>,<a href='mailto:584991843@qq.com'> Send an email </a>,<a href='http://www.baidu.com'>Go Baidu </a></string>
XML for TextView is defined as follows:
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/tv4"
android:textSize="24sp" >
</TextView>
Then you only need to set the TextView to clickable state in Activity:
tv4.setMovementMethod(LinkMovementMethod.getInstance());
4. Use SpannableString to implement hyperlinks (with various effects)
For more use of SpannableString, see another article:
SpannableString ss = new SpannableString(" Phone calls, text messages, e-mails, Go Baidu ");
ss.setSpan(new URLSpan("tel:18565554482"), 0, 3,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("smsto:18565554482"), 4, 7,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("mailto:584991843@qq.com"), 8, 11,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("http://www.baidu.com"), 12, 16,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//SpannableString Object is set to the TextView
tv3.setText(ss);
// Settings TextView Clickable
tv3.setMovementMethod(LinkMovementMethod.getInstance());
5. Implement with SpannableTextView (varied effects)
Set single 1 effect:
// Setup single span
SpannableTextView tv1 = (SpannableTextView) view.findViewById(R.id.tv1);
Span span1 =
new Span.Builder("ForegroundSpan, BackgroundSpan, and CustomTypefaceSpan")
.foregroundColor(R.color.purple_500)
.backgroundColor(R.color.green_500)
.typeface(mItalicFont)
.build();
tv1.setFormattedText(span1);
Set multiple effects overlay:
// Setup multiple spans
SpannableTextView tv2 = (SpannableTextView) view.findViewById(R.id.tv2);
List<Span> spans1 = new ArrayList<>();
spans1.add(new Span.Builder("ForegroundSpan")
.foregroundColor(R.color.red_500)
.build());
spans1.add(new Span.Builder("BackgroundSpan")
.backgroundColor(R.color.yellow_500)
.build());
spans1.add(new Span.Builder("ForegroundSpan and BackgroundSpan")
.foregroundColor(R.color.orange_500)
.backgroundColor(R.color.blue_500)
.build());
spans1.add(new Span.Builder("ForegroundSpan, BackgroundSpan, and CustomTypefaceSpan")
.foregroundColor(R.color.green_500)
.backgroundColor(R.color.indigo_500)
.typeface(mRegularFont)
.build());
tv2.setFormattedText(spans1);
Implement ununderlined hyperlinks:
Custom urlspan inherits URLSpan to remove underscores
// Customize urlspan Remove the underline
public class URLSpanNoUnderline extends URLSpan {
public URLSpanNoUnderline(String url) {
super(url);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
ds.setColor(Color.BLACK);
}
}
This article mainly introduces five ways to achieve Android TextView hyperlink source examples, more about Android to achieve TextView hyperlink articles please see the following links