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

Android EditText 控件屬性完整整理

編輯:關於 Android 開發

EditText 的繼承關係為:View → TextView → EditText。由於它具備大量屬性,以下整理出常用與重要的屬性供開發時查閱。

常用屬性速覽

android:hint="請輸入數字!" // 設定顯示在控件上的提示資訊
android:numeric="integer" // 設定只能輸入整數,若為小數則是 decimal
android:singleLine="true" // 設定單行輸入,設為 true 後文字不會自動換行
android:password="true" // 設定只能輸入密碼
android:textColor = "#ff8c00" // 字體顏色
android:textSize="20dip" // 大小
android:capitalize = "characters" // 以大寫字母書寫
android:textColorHighlight="#cccccc" // 被選取文字的底色,預設為藍色
android:textColorHint="#ffff00" // 設定提示資訊文字顏色,預設為灰色
android:textScaleX="1.5" // 控制字與字之間的間距
android:typeface="monospace" // 字型,可選 normal、sans、serif、monospace
android:background="@null" // 背景,此處為透明
android:layout_weight="1" // 權重,控制控件之間的比例
android:layout_gravity="center_vertical" // 設定控件顯示位置,預設 top,此處居中
et.setSelection(et.length()); // 將游標調整到最後一行

屬性詳細說明

  • android:autoLink:設定當文字為 URL 連結/email/電話號碼/map 時,是否顯示為可點擊的連結。可選值為 none/web/email/phone/map/all。
  • android:autoText:若設定,將自動執行輸入值的拼寫校正,於彈出輸入法並輸入時生效。
  • android:bufferType:指定 getText() 取得的文字類別。editable 類似 StringBuilder 可追加字元;spannable 則可在指定字元區域套用樣式。
  • android:capitalize:設定英文字母大寫類型,需彈出輸入法才能看到效果。
  • android:cursorVisible:設定游標為顯示或隱藏,預設顯示。
  • android:digits:設定允許輸入哪些字元,如「1234567890.+-*/% ()」。
  • android:drawableBottom/Left/Right/Top:分別在文字的下方、左邊、右邊、上方輸出一個 drawable(如圖片)。
  • android:drawablePadding:設定文字與 drawable 之間的間隔,可設為負數,需與上述方向屬性搭配使用。
  • android:ellipsize:設定文字過長時的顯示方式,可選 start(省略號在開頭)、end(結尾)、middle(中間)、marquee(跑馬燈橫向滾動)。
  • android:gravity:設定文字位置,如 center 使文字居中顯示。
  • android:hint:文字為空時顯示的提示資訊,可透過 textColorHint 設定顏色。
  • android:imeOptions:設定右下角 IME 動作,如 actionDone 會顯示「完成」。
  • android:inputType:設定文字類型,用以幫助輸入法顯示合適的鍵盤類型。
  • android:maxLength:限制顯示的文字長度,超出部分不顯示。
  • android:lines:設定文字的行數,即使第二行沒有資料也會佔位顯示。
  • android:maxLines:設定文字最大顯示行數,需與 width 或 layout_width 搭配,超出部分自動換行、超出行數則不顯示。
  • android:minLines:設定文字最小行數,類似 lines。
  • android:lineSpacingExtra/lineSpacingMultiplier:分別設定行間距與行間距倍數(如 1.2)。
  • android:password:以小點「.」顯示文字。
  • android:phoneNumber:設定為電話號碼的輸入方式。
  • android:shadowColor/shadowDx/shadowDy/shadowRadius:分別設定文字陰影的顏色、橫向與縱向起始座標、以及陰影半徑(一般設為 3.0 效果較佳)。
  • android:singleLine:設定單行顯示。若與 layout_width 搭配,文字無法完整顯示時會以「…」表示;設為 false 則文字自動換行。
  • android:text/textSize/textStyle/typeface:分別設定顯示文字、文字大小(建議使用 sp)、字形(bold/italic/bolditalic,可用「|」分隔多個)與字體(normal/sans/serif/monospace)。
  • android:height/maxHeight/minHeight/width/maxWidth/minWidth:分別設定文字區域的高、最大高、最小高、寬、最大寬、最小寬,支援 px/dp/sp/in/mm 等單位。
  • android:textAppearance:設定文字外觀,如「?android:attr/textAppearanceLargeInverse」,其中 ? 表示若系統有該外觀則使用,否則採用預設外觀。可選值包括 textAppearanceButton、textAppearanceInverse、textAppearanceLarge、textAppearanceLargeInverse、textAppearanceMedium、textAppearanceMediumInverse、textAppearanceSmall、textAppearanceSmallInverse。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved