Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 常用控件學習:TextView、EditText、Button 與選擇控制項

編輯:關於 Android 開發

Android 常用控件入門

Android 的人機介面由各種 View 與 ViewGroup 組成。掌握常見控件的使用方式,是建立良好使用者體驗的基礎。

TextView

TextView 是最基本的文字顯示元件。常用屬性包含 id、layout_width、layout_height、text、textSize 與 textColor。寬高常見值有 match_parent、fill_parent 與 wrap_content。

EditText

EditText 可讓使用者輸入文字,除了繼承 TextView 的屬性外,也常用 hint 屬性顯示提示文字。當使用者尚未輸入內容時,hint 會顯示提示,輸入後會自動消失。

ImageView

ImageView 用於顯示圖片。常用屬性包含 src 與 background。src 通常用來指定要顯示的圖片資源,background 則可設定背景圖片或背景顏色。

Button 與 ImageButton

Button 常用來顯示可點擊的文字按鈕,可透過 text 設定文字,也可透過背景色或背景圖自訂外觀。ImageButton 則更偏向以圖片呈現按鈕。

使用 Button 時,通常會透過 setOnClickListener 設定點擊事件。常見寫法包含匿名內部類、外部類別或 Activity 直接實作 OnClickListener。

AutoCompleteTextView 與 MultiAutoCompleteTextView

這兩個控件都能提供輸入時自動匹配的功能。AutoCompleteTextView 適合單一輸入欄位;MultiAutoCompleteTextView 則可輸入多筆內容,並在每筆後加上分隔符。

使用時通常需要設定 completionThreshold,並搭配 ArrayAdapter 等適配器提供候選資料。

ToggleButton、CheckBox 與 RadioButton

ToggleButton 具有開與關兩種狀態,可透過 textOn 與 textOff 設定不同狀態下顯示的文字。

CheckBox 則提供勾選與取消勾選的功能,常搭配 OnCheckedChangeListener 使用。

RadioButton 通常會與 RadioGroup 一起使用,形成單選群組,確保同一時間只能選擇一個選項。

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