編輯:關於 Android 開發
表單是 App 收集使用者資訊的主要途徑。本文按控件逐一梳理 Android 中最常用的幾組表單元素:Toast、TextView、Button、EditText、CheckBox、RadioButton/RadioGroup 與 ImageView,包含常用屬性、常用方法與可運行的代碼示例。

Toast 是一種輕量級的提示工具,可以顯示一行文本,對使用者的操作進行提示響應。
用法:Toast.makeText(context, text, time).show();
三個參數分別是:context(上下文)、text(顯示文本內容)、time(顯示時長)。時長有兩種常量:Toast.LENGTH_SHORT(短時間顯示)與 Toast.LENGTH_LONG(長時間顯示)。
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(ceshi.this, "你點擊了按鈕", Toast.LENGTH_SHORT).show();
}
});
常用屬性:
text:顯示的文本內容textColor:文本顏色textSize:字體大小singleLine:單行顯示(true / false)gravity:內容的重心位置drawableLeft / drawableRight / drawableTop / drawableBottom:分別在文字左、右、上、下顯示圖片
<TextView
android:layout_width="200dp"
android:layout_height="100dp"
android:background="#00ff00"
android:id="@+id/textView2"
android:layout_alignBottom="@id/f1"
android:layout_centerInParent="true"
android:text="來一碗魚丸粗面"
android:textColor="#f00"
android:textSize="20sp"
android:gravity="bottom|center"/>
常用方法:setText() 設置顯示文字;setTextColor() 設置文字顏色(參數為顏色值);setTextSize() 設置文字尺寸(參數 1 為單位,參數 2 為大小);getText() 獲取顯示的文字。
Button 繼承自 TextView,因此擁有 TextView 的全部屬性,事件處理方式則參照上文 Toast 示例中的 setOnClickListener()。
EditText 同樣繼承自 TextView,在此基礎上增加了輸入相關的能力。
常用屬性:
inputType:限制能夠輸入的文本類型hint:顯示的提示(值為 string)maxLength:能夠輸入的最大長度(值為 int)focusable:能否獲取焦點(值為 boolean)lines:顯示的行數;maxLines:最大顯示行數(值為 int)enabled:是否可用(值為 boolean)
<EditText
android:id="@+id/edittext_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼!"
android:textSize="16sp"
android:textColor="#f00"
android:textColorHighlight="#a00000ff"
android:drawableLeft="@mipmap/ic_launcher"
android:inputType="textPassword" />
常用方法:setHint()、setInputType()、setFocusable()、setEnabled()、isEnabled()。
獲取輸入內容與監聽文本變化:
edittext1.getText().toString(); // 獲取 EditText 的文本值
edittext1.addTextChangedListener(watcher); // 文本監聽
private TextWatcher watcher = new TextWatcher() {
/* 文本改變之前 */
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/* 當文本改變的時候 */
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
/* 文本改變之後 */
@Override
public void afterTextChanged(Editable s) {
// 獲取已經輸入的文字總數
int currentCount = s.length();
// 計算還能輸入多少個字符,記住,數字不能直接寫
textview1.setText(String.valueOf(max - currentCount));
}
};
這種「實時字數統計」是限字輸入框的標準做法,常見於短信、評論與個性簽名等場景。

CheckBox 繼承自 TextView,常用屬性包括 TextView 的全部屬性,再加上 button(復選指示按鈕)與 checked(是否選中)。
常用方法:setChecked() 設置是否選中、isChecked() 查詢是否選中、setOnCheckedChangeListener() 設置選中改變監聽。
下面是一個帶「全選」功能的多選示例,示範了幾個實務上必然遇到的細節。
<!-- 布局 -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="https://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="平時喜歡做什麼事情?" />
<CheckBox
android:id="@+id/checkboxall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textviewtitle"
android:layout_alignTop="@id/textviewtitle"
android:layout_toRightOf="@id/textviewtitle"
android:text="全選" />
<!-- 內容的 CheckBox -->
<CheckBox android:id="@+id/checkbox1" android:text="玩游戲"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle" android:layout_marginRight="80dp" />
<CheckBox android:id="@+id/checkbox2" android:text="學習"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_toRightOf="@+id/checkbox1" />
<CheckBox android:id="@+id/checkbox3" android:text="敲代碼"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/checkbox1" />
<CheckBox android:id="@+id/checkbox4" android:text="跑步"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/checkbox2"
android:layout_toRightOf="@+id/checkbox1" />
<CheckBox android:id="@+id/checkbox5" android:text="游泳"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/checkbox3" />
<CheckBox android:id="@+id/checkbox6" android:text="睡覺"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/checkbox4"
android:layout_toRightOf="@+id/checkbox1" />
<TextView
android:id="@+id/textviewinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox5"
android:layout_marginTop="20dp"
android:text="已選擇:" />
</RelativeLayout>
package com.dc.work3;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.LinkedList;
import java.util.List;
public class MainActivity2s extends AppCompatActivity {
private CheckBox checkboxall;
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private CheckBox checkBox4;
private CheckBox checkBox5;
private CheckBox checkBox6;
private TextView textviewinfo;
private List<String> checkedStr;
// 操作取消一個時,全選取消,這個變量標記是否是用戶點擊
private boolean checkFoUser = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2);
checkboxall = (CheckBox) findViewById(R.id.checkboxall);
checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
checkBox4 = (CheckBox) findViewById(R.id.checkbox4);
checkBox5 = (CheckBox) findViewById(R.id.checkbox5);
checkBox6 = (CheckBox) findViewById(R.id.checkbox6);
textviewinfo = (TextView) findViewById(R.id.textviewinfo);
checkBox1.setOnCheckedChangeListener(changeListener);
checkBox2.setOnCheckedChangeListener(changeListener);
checkBox3.setOnCheckedChangeListener(changeListener);
checkBox4.setOnCheckedChangeListener(changeListener);
checkBox5.setOnCheckedChangeListener(changeListener);
checkBox6.setOnCheckedChangeListener(changeListener);
checkboxall.setOnCheckedChangeListener(changeListener);
checkedStr = new LinkedList<>();
}
public CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
case R.id.checkbox2:
case R.id.checkbox3:
case R.id.checkbox4:
case R.id.checkbox5:
case R.id.checkbox6:
String str = buttonView.getText().toString();
if (isChecked) {
checkedStr.add(str);
} else {
checkedStr.remove(str);
}
// 臨時摘掉全選的監聽,避免 setChecked 反向觸發造成無限迴圈
checkboxall.setOnCheckedChangeListener(null);
if (checkBox1.isChecked() && checkBox2.isChecked() && checkBox3.isChecked()
&& checkBox4.isChecked() && checkBox5.isChecked() && checkBox6.isChecked()) {
// 如果全部選中,把全選按鈕也選中
checkboxall.setChecked(true);
} else {
// 否則全選按鈕不選中
checkboxall.setChecked(false);
}
checkboxall.setOnCheckedChangeListener(changeListener);
break;
case R.id.checkboxall:
if (checkFoUser) {
checkBox1.setChecked(isChecked);
checkBox2.setChecked(isChecked);
checkBox3.setChecked(isChecked);
checkBox4.setChecked(isChecked);
checkBox5.setChecked(isChecked);
checkBox6.setChecked(isChecked);
break;
}
}
StringBuffer sb = new StringBuffer();
for (String s : checkedStr) {
sb.append(s + ",");
}
if (sb.length() > 0) {
// 設置長度為長度-1,去除最後的「,」
sb.setLength(sb.length() - 1);
}
textviewinfo.setText("已選擇:" + sb.toString());
}
};
}
這段代碼裡最值得記住的是「摘監聽再掛回」的技巧:當子項變化需要反向更新全選框時,setChecked() 會觸發全選框自己的監聽,從而把所有子項一併重置。先把監聽置空、改完狀態、再掛回去,就能避免這個迴路。

RadioButton 繼承自 TextView,單獨使用時行為與 CheckBox 相同;但通常會與 RadioGroup 搭配,形成互斥的單選效果。
RadioGroup 繼承自 LinearLayout,擁有 LinearLayout 的全部屬性。常用方法有:setOnCheckedChangeListener() 監聽內部單選按鈕的選中變化;check() 選中指定 id 的子項;getCheckedRadioButtonId() 獲取當前選中 RadioButton 的 id;clearCheck() 清除所有選中。
常用屬性:
src:指定要顯示的圖片adjustViewBounds:控件大小僅顯示到圖片的範圍maxHeight / maxWidth:最大高度與最大寬度scaleType:縮放類型,是最容易出錯的一組屬性scaleType 的常見取值與行為:
fitStart:等比例自適應大小,靠起始端顯示fitEnd:等比例自適應大小,靠末尾顯示fitXY:不保持比例,直接拉伸填滿控件的 x、y 方向fitCenter:等比例自適應大小,居中顯示center:按原圖片大小在中心顯示,不縮放centerCrop:等比例縮放至充滿控件,超出部分裁掉centerInside:圖片小於控件時縮放並居中顯示,否則等比例縮放後居中matrix:不縮放,從左上角繪製,此時可透過 setImageMatrix() 對圖片做任意變換把這些控件組合起來,就構成了 Android 中最典型的表單界面。實務上真正決定體驗好壞的,往往不是控件本身,而是 inputType 是否選對、hint 是否寫清楚、以及錯誤提示是否及時。
仿 QQ 圖片選擇器:Android 本地圖片選擇功能實作
做一款 APP 時,若需要選擇本地圖片,首先想到的往往是系統相冊。但 Android 手機型號五花八門,加上手機像素不斷提升、大圖無法返回等異常因素,導致適配各機型相當困
Material Design NavigationView 抽屜導航菜單
Material Design NavigationView 抽屜導航菜單NavigationView 是 Material Design 中常見的抽屜選單元件,通常與
我的第一節 Android 課:從系統框架理解安卓
今天是我程序猿之路的第一節 Android 課程。安卓課程對我而言是個興趣班,本專業是 Java 開發,因為想親手做出一個屬於自己的手機 App,便多學了這項技能。十八般
動手實作 RecyclerView:拖曳排序與側滑刪除的完整教學
動手實作 RecyclerView:拖曳排序與側滑刪除的完整教學 先看成品效果,這篇文章會帶你一步步打造可拖曳排序、可側滑刪除的清單: 我們參考開源範例 Android