textView 顯示和html的元素控件與進行超鏈接


1.這些類似html標簽可以用Html.fromHtml方法將html標簽字符串轉化成CharSequence對象,然后再TextView中進行設置:

如:

在.xml文件中

<TextView
android:id="@+id/textview1"
android:padding="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

在.java文件中

textView1 = (TextView) findViewById(R.id.textview1);

String html = "<font color='red'>I love android </font><br>";
html+="<font color='#0000ff'><big><i>I love android</i> </big></font><p>";
html+="<big><a href='http://www.baidu.com'>百度</a></big>";
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());//點擊的時候產生超鏈接

 

2.如果想在顯示文本中將URL地址,郵箱地址,電話超鏈接的效果可以使用android::autoLink設置.

如:

在.xml文件中

<TextView
android:id="@+id/textview2"
android:padding="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="20sp"
android:text="@string/link_text"
/>

 

strings.xml中:

<string name="link_text"><a href="tel:18870076184">打電話</a></string>

在.java文件中:

String text = "我的URL:http://www.sina.com\n";
text+="我的email:abcd@126.com\n";
text+="我的電話:18870076184";
textView2.setText(text);
textView2.setMovementMethod(LinkMovementMethod.getInstance());

 運行結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM