UI組件TextView
屬性:android:autoLink
我們用一個實例來解釋這個屬性
首先在strings.xml裏寫出我們需要的字符串
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">TextViewTest</string>
<string name="webUrl">百度:http://baidu.com</string>
<string name="email">我的:296463139@qq.com</string>
<string name="phoneNumber">電話號碼:10086</string>
<string name="autoSAll">百度:http://baidu.com 我的:296463139@qq.com</string>
</resources>
然後在我們的main.xml裏聲明屬性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="web"
android:text="@string/webUrl" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="email"
android:text="@string/email" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="phone"
android:text="@string/phoneNumber" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/autoSAll" />
<TextView
android:id="@+id/tvId"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
/////////////Activity////////////////////
package cn.class3g.activity;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class TextViewTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) this.findViewById(R.id.tvId);
String htmlStr = "<font size='30' color='#00FF22'>我</font>愛<b>你</b>"
+ "<a href='baidu.com'>百度</a>";
tv.setText(Html.fromHtml(htmlStr));
}
}
注意:
android:autoLink=”email” :會出現unsupported action,可能是模擬器bug,須探究
另外使用Html.fromHtml時,超鏈接只具備外觀,不能跳轉
效果圖: