編輯:關於 Android 開發
本篇進入第二組 UI 組件的學習——TextView 及其子類。TextView 是 Android 中最基礎的文本顯示組件,掌握它以及由它派生的 EditText、Button 等控件,是構建任何界面的第一步。
TextView 直接繼承自 View,同時也是 EditText(允許用戶編輯文本)與 Button 兩個組件的父類,它本身則不允許用戶編輯內容。其作用就是在界面上顯示文本,類似於桌面開發中的 JLabel。
下面是一個綜合示例,展示了字號、尾部圖片、省略、大小寫轉換、自動鏈接、陰影、密碼框以及可勾選文本等多種用法:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 設置字號為 20pt,在文本框結尾處繪製圖片 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30pt"
android:text="我愛Java"
android:textSize="20pt"
android:drawableEnd="@drawable/doge"
android:drawableRight="@drawable/doge"/>
<!-- 設置中間省略,所有字母大寫 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="我愛Java 我愛Java 我愛Java 我aaa Java"
android:ellipsize="middle"
android:textAllCaps="true"/>
<!-- 對郵件/電話增加鏈接 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="郵件是somebody@163.com,電話是222266666"
android:autoLink="email|phone"/>
<!-- 設置文字顏色/大小,並使用陰影 -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="測試文字"
android:shadowColor="#00f"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:textColor="#f00"
android:textSize="18pt"/>
<!-- 測試密碼框 -->
<TextView
android:id="@+id/passwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:password="true"/>
<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="可勾選的文本"
android:checkMark="@drawable/doge"/>
</LinearLayout>

接著是一個圓角邊框與漸變背景的 TextView 實例。首先在兩個 XML 文件中定義背景。bg_border.xml 定義了一個帶紅色邊框、透明背景的形狀:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 設置背景色為透明色 -->
<solid android:color="#0000" />
<!-- 設置紅色邊框 -->
<stroke android:width="4px" android:color="#f00" />
</shape>
bg_border1.xml 則定義了四角不同圓角半徑、粉紅邊框並帶 sweep 漸變效果的形狀:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" android:shape="rectangle">
<!-- 指定圓角矩形的 4 個圓角的半徑 -->
<corners android:topLeftRadius="20px"
android:topRightRadius="5px"
android:bottomRightRadius="20px"
android:bottomLeftRadius="5px"/>
<!-- 指定邊框線條的寬度和顏色 -->
<stroke android:width="4px" android:color="#f0f" />
<!-- 指定使用 sweep 類型的漸變背景,顏色從紅到綠再到藍 -->
<gradient android:startColor="#f00"
android:centerColor="#0f0"
android:endColor="#00f"
android:type="sweep"/>
</shape>
提示:新增 shape 這類 XML 文件時,Android Studio 預設會將它放到 layout 目錄下且無法直接移動,此時需要手動將文件剪切到 drawable 文件夾中。接著新增一個布局文件 border.xml 並透過 android:background 引用這兩個背景:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="帶邊框的文本"
android:textSize="24pt"
android:background="@drawable/bg_border"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="圓角邊框/漸變背景的文本"
android:textSize="24pt"
android:background="@drawable/bg_border1"/>
</LinearLayout>
若引用背景時出現紅色感歎號報錯,並在運行時拋出空指針異常,通常都是由於背景文件放置的目錄不對或引用路徑有誤所致,需仔細核對 drawable 中的文件名與引用是否一致。

EditText 與 TextView 最大的區別在於它能接受用戶輸入。它最重要的屬性是 inputType,作用類似於 HTML 中 input 元素的 type 屬性,用於將輸入框指定為不同類型,從而觸發不同的軟鍵盤。下面是一個對用戶較為友好的輸入界面:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TextView android:layout_width="match_parent" android:layout_height="32sp" />
<TableRow>
<TextView ... android:text="用戶名:" />
<EditText ... android:hint="請填寫登陸賬號" android:selectAllOnFocus="true"/>
</TableRow>
<TableRow>
<TextView ... android:text="密碼:" />
<!-- numberPassword 表明只能接受數字密碼 -->
<EditText ... android:inputType="numberPassword"/>
</TableRow>
<TableRow>
<TextView ... android:text="年齡:" />
<!-- number 表明是數值輸入框 -->
<EditText ... android:inputType="number"/>
</TableRow>
<TableRow>
<TextView ... android:text="生日:" />
<!-- date 表明是日期輸入框 -->
<EditText ... android:inputType="date"/>
</TableRow>
<TableRow>
<TextView ... android:text="電話號碼:" />
<!-- phone 表明是輸入電話號碼的輸入框 -->
<EditText ... android:hint="請填寫您的電話號碼"
android:selectAllOnFocus="true" android:inputType="phone"/>
</TableRow>
<Button ... android:text="注冊"/>
</TableLayout>

切換不同輸入框時,系統會根據 inputType 自動切換為對應的鍵盤布局,例如數字鍵盤、電話鍵盤或日期選擇器。
Button 可以指定按壓與放開時顯示不同圖片,也可以使用 9Patch 圖片作為背景(SDK 的 tools 目錄下的 draw9patch.bat 可設定可伸縮區域,防止圖片變形)。下面是一個簡單示例,界面布局如下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_marginTop="80px" ... />
<!-- 文字帶陰影的按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文字帶陰影的按鈕"
android:textSize="12pt"
android:shadowColor="#aa5"
android:shadowRadius="1"
android:shadowDx="5"
android:shadowDy="5"/>
<!-- 普通文字按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/doge"
android:text="普通按鈕"
android:textSize="10pt" />
<!-- 帶文字的圖片按鈕 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:textSize="11px"
android:text="帶文字的圖片按鈕"/>
</LinearLayout>
其中第三個按鈕引用了一個 selector 文件,用於指定按下與放開時的不同圖片:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 指定按鈕按下時的圖片 -->
<item android:state_pressed="true" android:drawable="@drawable/three"/>
<!-- 指定按鈕放開時的圖片 -->
<item android:state_pressed="false" android:drawable="@drawable/four"/>
</selector>

單選鈕(RadioButton)與復選框(CheckBox)相比普通按鈕多了可選中的功能,可額外指定 android:checked 屬性來設定初始是否被選中。需要特別注意:如果在 XML 布局中定義了一組單選鈕並默認勾選了第一個,則必須為該組中的每個單選鈕都指定 android:id,否則這組單選鈕將無法正常工作。
<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView ... android:layout_marginTop="100px"/>
<TableRow>
<TextView ... android:text="性別"/>
<!-- 定義一組單選鈕 -->
<RadioGroup android:id="@+id/rg" android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<RadioButton android:id="@+id/male" ... android:text="男" android:checked="true"/>
<RadioButton android:id="@+id/female" ... android:text="女"/>
</RadioGroup>
</TableRow>
<TableRow>
<TextView ... android:text="喜歡的顏色:"/>
<LinearLayout android:orientation="vertical" ...>
<CheckBox ... android:text="紅色" android:checked="true"/>
<CheckBox ... android:text="藍色"/>
<CheckBox ... android:text="綠色"/>
</LinearLayout>
</TableRow>
<TextView android:id="@+id/show" ... />
</TableLayout>
為了監聽勾選狀態的變化,可為 RadioGroup 添加 OnCheckedChangeListener 監聽器:
public class RadioButtonAndCheckBox extends AppCompatActivity {
RadioGroup rg;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_and_check_box);
rg = (RadioGroup) findViewById(R.id.rg);
show = (TextView) findViewById(R.id.show);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String tip = checkedId == R.id.male ? "您的性別是男" : "您的性別是女";
show.setText(tip);
}
});
}
}

狀態開關按鈕(ToggleButton)與開關(Switch)通常用於切換程序中的某種狀態。下面是一個動態控制布局的實例:透過切換按鈕,讓 LinearLayout 在水平與垂直布局之間切換。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView ... android:layout_marginTop="150px"/>
<!-- 定義一個 ToggleButton 按鈕 -->
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:checked="true"/>
<Switch
android:id="@+id/switcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="橫向排列"
android:textOn="縱向排列"
android:thumb="@drawable/three"
android:checked="true"/>
<!-- 定義一個可動態改變方向的線性布局 -->
<LinearLayout
android:id="@+id/test"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:layout_width="200px" android:layout_height="120px" android:text="測試按鈕1"/>
<Button android:layout_width="200px" android:layout_height="120px" android:text="測試按鈕2"/>
<Button android:layout_width="200px" android:layout_height="120px" android:text="測試按鈕3"/>
</LinearLayout>
</LinearLayout>
在 Java 代碼中為 ToggleButton 與 Switch 綁定同一個監聽器,當選中狀態改變時動態調整 LinearLayout 的方向:
public class toggleButton_index extends AppCompatActivity {
ToggleButton toggle;
Switch switcher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toggle_button_index);
toggle = (ToggleButton) findViewById(R.id.toggle);
switcher = (Switch) findViewById(R.id.switcher);
final LinearLayout test = (LinearLayout) findViewById(R.id.test);
CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button, boolean isChecked) {
if (isChecked) {
test.setOrientation(LinearLayout.VERTICAL);
toggle.setChecked(true);
switcher.setChecked(true);
} else {
test.setOrientation(LinearLayout.HORIZONTAL);
toggle.setChecked(false);
}
}
};
toggle.setOnCheckedChangeListener(listener);
switcher.setOnCheckedChangeListener(listener);
}
}


AnalogClock 顯示模擬時鐘,TextClock(需 API 17 以上)顯示數字時鐘,可展示到秒。示例如下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal">
<TextView ... android:layout_marginTop="120px"/>
<!-- 定義模擬時鐘 -->
<AnalogClock android:layout_width="wrap_content" android:layout_height="wrap_content" />
<!-- 定義數字時鐘 -->
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20pt"
android:textColor="#f0f"
android:format12Hour="yyyy年MM月dd日 H:mma EEEE"
android:drawableEnd="@drawable/four"
android:drawableRight="@drawable/four"/>
<!-- 定義模擬時鐘,並使用自定義表盤/時針圖片 -->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:dial="@drawable/clock"/>
</LinearLayout>

下面的程序實現:用戶單擊按鈕開始計時,超過 20 秒自動停止計時(布局文件較簡單此處省略):
public class chronometer_index extends AppCompatActivity {
Chronometer ch;
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chronometer_index);
ch = (Chronometer) findViewById(R.id.chr);
start = (Button) findViewById(R.id.begin);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ch.setBase(SystemClock.elapsedRealtime());// 設置開始計時時間
ch.start();// 啟動計時器
start.setEnabled(false);
}
});
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
// 如果從開始計時到現在超過了 20s
if (SystemClock.elapsedRealtime() - ch.getBase() > 20 * 1000) {
ch.stop();
start.setEnabled(true);
}
}
});
}
}

以上就是 TextView 及其相關子類的常見用法。下一篇將進入第三組 UI 組件:ImageView 及其子類的學習。
我的 Android 第二課:專案結構、清單檔、DDMS 與打電話案例
我的 Android 第二課:專案結構、清單檔、DDMS 與打電話案例 各位好,小編又來分享知識了。上一篇筆記講解了 Android 工具與 SDK 的設定,今天則要帶大
用 Okio 簡化 I/O:Source、Sink 與 Buffer 的設計之美
Okio 是 Square 開發的函式庫,它補足了 java.io 與 java.nio 的不足,讓「讀取、儲存、處理資料」這件事變得更方便、更快速。OkHttp 的底層
Android 源碼解析——SwipeMenuListView 仿 QQ 聊天左滑菜單
緒論: 最近在看一些 Android 進階書籍,這裡也給大家推薦幾本適合進階的書,相信會有所幫助: 1.《Android 群英傳》—徐宜生 2.《Android 開發藝術
在 Eclipse 中安裝 Genymotion 模擬器插件的完整步驟
Genymotion 的啟動速度與流暢度遠勝原生模擬器,是早期 Android 開發時相當受歡迎的方案。以下記錄在 Eclipse(ADT)中安裝它的流程。 一、安裝