基礎控件------TextView控件3(識別特殊文本)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="phone"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="phone:13078582231"
        android:textSize="18sp"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="web:www.baidu.com"
        android:textSize="18sp"
         />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="email"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="email:[email protected]"
        android:textSize="18sp" />

</LinearLayout>
android提供了六個autoLink以供使用:web(匹配網址)  phone(匹配電話)
email(匹配郵箱)   all(匹配所有)、none
除了可以在屬性中設置實現識別特殊文本還可以在java代碼中實現
xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="10dp"
        android:text="web:www.baidu.com"
        android:textSize="18sp"
        />

</LinearLayout>
java文件如下:
private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextView = (TextView)findViewById(R.id.test);
    mTextView.setAutoLinkMask(Linkify.WEB_URLS);//識別url類型文本
    mTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章