在程式開發裡,文字的顯示以及輸入為重要得一環,TextView是一個基本元件,不僅可以用main.xml檔來控制,也可以在java檔內用method來控制。

TextView常用屬性及對應method表:

layout13.jpg  

小試身手小範例:

此範例會運用到三個<TextView>元件,其中兩個<TextView>元件做為網頁連結的書籤,內容為Google(“www.google.com”)以及Apple(“www.apple.com”)兩筆資料,最後一個為可撥話的手機號碼。當TextView加入autoLink屬性後,若文字當中有連結(ex. E-mail Map連結、電話號碼、網頁),即可自動連結並對應到適合的應用程式。

autoLink屬性表:

TextView01.jpg  

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="google - www.google.com"

    android:autoLink="web"

    android:textSize="20sp"

    />

    <TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="apple - www.apple.com"

    android:autoLink="web"

    android:textSize="20sp"

    />

    <TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="Tom - 0912345678"

    android:autoLink="phone"

    android:textSize="20sp"

    />

</LinearLayout>

p.s由於使用web需要在AndroidManifest.xml中加入INTENT權限,不然會出現錯誤畫面喔!

(記得加上黃標那行)

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.sam.test"

      android:versionCode="1"

      android:versionName="1.0">

    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".LinearLayout01"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    <uses-permission android:name="android.permission.INTENT"/>

    </application>

</manifest>

TextView02.jpg  

按下apple後

TextView03.jpg  

按下Tom的電話之後

TextView04.jpg  

 

arrow
arrow

    S 發表在 痞客邦 留言(0) 人氣()