昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类。首先,看一下其运行效果:
?
?
要给 TextView 加上效果,方式主要有几种:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all" 取值见 Reference 文档的 TextView
android:text="@string/link_text_auto"
/>
<string name="link_text_manual"><b>text2:</b> This is some other text, with a <a href="http://www.google.com">link</a> specified via an <a> tag. Use a \"tel:\" URL to <a href="tel:4155551212">dial a phone number</a> </string>
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());
SpannableString ss = new SpannableString("text4: Click here to dial the phone."); ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); TextView t4 = (TextView) findViewById(R.id.text4); t4.setText(ss); t4.setMovementMethod(LinkMovementMethod.getInstance());
?
?
?完整的代码见 ApiDemo 吧,下面我提几点需要注意的: