編輯:開發入門
范例說明
在「人機介面」這個主題的開頭,就先用最經典的 Hello World 精神,跟 TextView 這個文字標籤打第一次照面。這個範例會示範三件事:在 Layout 裡建立 TextView 物件、在 res/values/strings.xml 裡定義字串常數,最後透過 setText() 在程式啟動時動態換掉文字。
用 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>
<?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>
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);
}
}
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_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 開發範例的學習筆記,若有錯誤之處歡迎指正。
Android 上的 XML 與 JSON 實戰(下):SAX 串流解析與 JSON 物件解析的取捨
先說清楚這個範例的定位 有個前提必須先講明白:這支範例程式並不會真的定時從網路抓資料回來。所有的 feed 都放在專案的 res/raw 資源目錄裡,所以程式關注的重點是
Android 簡訊程式實作:TinySMS 與 SmsManager 傳送機制
以 TinySMS 理解簡訊功能 簡訊是手機最基礎的應用之一。本文以 TinySMS 為例,說明如何建立一個簡易的 Android 簡訊介面,並使用 SmsManager
Android 開發入門(三):動手做一個簡易手電筒 App
建立第一個 Android 應用 本文以一個修改自 Hello Android 的範例為基礎,示範如何建立最簡單的 Android 應用。這個範例會把畫面背景改為白色,讓
搞懂 Android 的置中:Button 與 Gallery 為什麼會對齊
從上圖可以看到,Button 與 Gallery 採用的對齊方式是「置中對齊」,也就是 Button 與它的 Parent 容器中心對齊,而不是貼齊某個邊緣。 An