Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 開發入門

Android 人機介面入門:TextView 標籤的使用與 setText 各種用法

編輯:開發入門

范例說明

在「人機介面」這個主題的開頭,就先用最經典的 Hello World 精神,跟 TextView 這個文字標籤打第一次照面。這個範例會示範三件事:在 Layout 裡建立 TextView 物件、在 res/values/strings.xml 裡定義字串常數,最後透過 setText() 在程式啟動時動態換掉文字。

版面檔:res/layout/main.xml

用 android:id 為 TextView 命名為 myTextView01。要注意的是新版寫法與早期版本有差異,特別是屬性名稱的大小寫:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
    android:id="@+id/widget35"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="https://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/myTextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/str_1"
        android:layout_x="61px"
        android:layout_y="69px" />
</AbsoluteLayout>

字串資源:res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Demo03_01!</string>
    <string name="app_name">Demo03_01</string>
    <string name="str_1">我是存放在 strings.xml 裡的「字串1」</string>
</resources>

主程式:Demo03_01.java

package org.demo03_01;

import android.app.Activity;
import android.os.Bundle;
// 必須 import widget.TextView,才能在程式中宣告 TextView 物件
import android.widget.TextView;

public class Demo03_01 extends Activity {

    private TextView mTextView01;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 載入 main.xml,此時 myTextView01 顯示的是 str_1
        setContentView(R.layout.main);

        mTextView01 = (TextView) findViewById(R.id.myTextView01);

        String str_2 = "歡迎來到 Android 的 TextView 世界...";
        mTextView01.setText(str_2);
    }
}

setText 的五種多載形式

TextView 的 setText() 支援以下幾種呼叫方式:

public final void setText(CharSequence text)
public final void setText(int resid)
public void setText(CharSequence text, TextView.BufferType type)
public final void setText(int resid, TextView.BufferType type)
public final void setText(char[] text, int start, int len)

其中 int resid 版本接受的是資源 ID,也就是 R.string.xxx;BufferType 版本則用來控制 TextView 內部如何保存文字(normal、editable、spannable),需要讓使用者編輯或要套用樣式時才用得到。

用 char 陣列輸出的例子

最後一種多載比較特別:第一個參數是 char 陣列作為資料來源,第二個參數是起始索引,第三個參數是取幾個元素。

char[] char_1 = new char[5];
char_1[0] = 'D';
char_1[1] = 'a';
char_1[2] = 'v';
char_1[3] = 'i';
char_1[4] = 'd';

mTextView01.setText(char_1, 1, 3);

輸出結果是 avi——從索引 1 開始,取 3 個元素。

兩個常見誤會

第一,TextView 不支援 HTML 標籤。即使你寫成這樣:

mTextView01.setText("<a href=\"https://example.com/\">戴維的博客</a>");

畫面上出現的也只會是那一串純文字,標籤不會被解析。

第二,autoLink 可以救回一部分。撇開 HTML 標籤不談,如果在 TextView 上加上 android:autoLink="all",那麼本文中出現的網址就會自動變成可點擊的連結:

<TextView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="all"
    android:text="請訪問部落格: https://example.com/MyBlog/" />

真的需要顯示 HTML 樣式(粗體、連結、換行)時,正確做法是用 Html.fromHtml() 先把 HTML 字串轉成 Spanned,再交給 setText()。

本文整理自 Android SDK 開發範例的學習筆記,若有錯誤之處歡迎指正。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved