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

Android EditText 屬性大全:輸入框各種設定一次搞懂

編輯:關於 Android 程式設計

做為 Android 中最常用的輸入元件,EditText 擁有大量可調校的屬性。本文把常見設定整理成一份 handy 的參考清單,方便開發時隨查隨用。

多行輸入的基本設定

android:id="@+id/editSms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/saveSms"
android:inputType="textMultiLine"
android:gravity="left|top"
android:minLines="6" />

幾個關鍵屬性說明如下:

android:inputType="textMultiLine" 讓輸入框可以顯示多行。
android:gravity="left|top" 游標輸入時定位在左上角。
android:minLines="6" 最少顯示 6 行。
android:layout_gravity="center_vertical" 設定元件顯示位置,預設為 top,這裡改為置中,也可設為 bottom。
android:hint="請輸入數字!" 設定佔位提示文字。
android:numeric="integer" 限制只能輸入整數,若要小數則改用 decimal。
android:singleLine="true" 強制單行輸入,設為 true 後文字不會自動換行。
android:gray="top" 多行時游標停在第一行第一個位置。
et.setSelection(et.length()); 把游標移到最後一行。

外觀與文字樣式

android:textColor="#ff8c00" 設定字體顏色。
android:textStyle="bold" 字形樣式,可選 bold、italic、bolditalic。
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:autoLink 當文字為網址、email、電話或地圖時顯示為可點擊連結,可選值 none/web/email/phone/map/all。
android:autoText 自動執行拼字校正,需彈出輸入法時才有效果。
android:bufferType 指定 getText() 取得的文字類別,editable 類似 StringBuilder 可追加字元,spannable 則可對指定字元套用樣式。
android:capitalize 設定英文大寫類型,需彈出輸入法才看得到效果。
android:cursorVisible 控制游標顯示或隱藏,預設顯示。
android:digits 限制可輸入哪些字元,例如 "1234567890.+-*/% ()"。
android:drawableBottom 在文字下方輸出圖片;若指定顏色會把背景設為該色並覆蓋 background。
android:drawableLeft 在文字左側輸出圖片。
android:drawablePadding 設定文字與圖片間距,需搭配 drawableLeft/Right/Top/Bottom 使用,可為負數。
android:drawableRight 在文字右側輸出圖片。
android:drawableTop 在文字上方輸出圖片。
android:ellipsize 文字過長時的顯示方式,start 省略號在開頭、end 在結尾、middle 在中間、marquee 以跑馬燈方式橫向捲動。
android:freezesText 保存文字內容與游標位置。
android:gravity 設定文字位置,例如 center 為置中。
android:imeOptions 設定右下角 IME 動作,例如 actionDone 會顯示「完成」而非預設的 Enter 符號。
android:includeFontPadding 文字是否包含頂部與底部額外空白,預設 true。
android:inputType 設定文字類型,用以提示輸入法顯示合適的鍵盤。
android:linksClickable 即使設定了 autoLink,仍可控制連結是否可點擊。
android:marqueeRepeatLimit ellipsize 為 marquee 時的重複捲動次數,設為 marquee_forever 表示無限次。
android:ems 將元件寬度設為 N 個字元寬。
android:maxEms/minEms 限制元件寬度的最大/最小字元數。
android:maxLength 限制顯示的文字長度,超出部分不顯示。
android:lines 設定文字行數。
android:maxLines 最大顯示行數,超出部分自動換行後不再顯示。
android:minLines 最小行數。
android:lineSpacingExtra 行間距。
android:lineSpacingMultiplier 行間距倍數,例如 "1.2"。
android:password 以小圓點 "." 顯示文字。
android:phoneNumber 設定為電話號碼輸入方式。
android:scrollHorizontally 文字超出寬度時是否出現橫向捲軸。
android:shadowColor/shadowDx/shadowDy/shadowRadius 設定文字陰影的顏色、位移與模糊半徑,半徑設 3.0 效果通常較佳。
android:singleLine 單行顯示,與 layout_width 搭配時文字過長會以 "…" 表示。
android:text 設定顯示文字。
android:textSize 文字大小,建議使用 sp 單位,例如 "15sp"。
android:textStyle 字形 [bold 0、italic 1、bolditalic 2],可用 "|" 分隔多個。
android:typeface 字型,必須是 normal 0、sans 1、serif 2、monospace 3 之一。
android:height/maxHeight/minHeight 文字區域的高度與最大、最小高度。
android:width/maxWidth/minWidth 文字區域的寬度與最大、最小寬度。
android:textAppearance 文字外觀,例如 "?android:attr/textAppearanceLargeInverse",開頭的問號代表若系統有該外觀則採用,否則使用預設。

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