安卓開發(fā)入門教程-UI控件_EditText

關(guān)注 安卓007 ,免費獲取全套安卓開發(fā)學(xué)習(xí)資料

什么是EditText

EditText是用于進(jìn)行文本輸入的UI控件.

基礎(chǔ)樣例

1.普通輸入

效果圖

代碼

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是EditText" />

2.設(shè)置字號

效果圖

代碼

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是20號字體的EditText"
    android:textSize="20sp" />

3.設(shè)置顏色

效果圖

代碼

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是紅色的EditText"
    android:textColor="#FF0000" />

4.設(shè)置加粗

效果圖

代碼

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="我是加粗的EditText"
    android:textStyle="bold" />

5.設(shè)置提示內(nèi)容

效果圖

代碼

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="我是提示內(nèi)容" />

6.輸入密碼

效果圖


代碼

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="請輸入密碼"
    android:inputType="textPassword" />

基礎(chǔ)樣例完整源代碼

https://gitee.com/cxyzy1/editTextDemo

常用屬性說明

屬性名 用途
android:layout_width 設(shè)置控件寬度,可設(shè)置為:match_parent(和父控件一樣),wrap_content(按照內(nèi)容自動伸縮),設(shè)置固定值(如200dp)
android:layout_height 設(shè)置控件高度,可設(shè)置為:match_parent(和父控件一樣),wrap_content(按照內(nèi)容自動伸縮),設(shè)置固定值(如200dp)
android:gravity 控件內(nèi)對齊方式
android:background 設(shè)置背景,可以是色值(如#FF0000)或圖片等
android:visibility 可選值: visible(顯示), invisible(隱藏,但是仍占據(jù)UI空間),gone(隱藏,且不占UI空間)
android:text 設(shè)置文本內(nèi)容
android:textSize 設(shè)置字號
android:textColor 設(shè)置顏色
android:textStyle 設(shè)置字體樣式,可選值:normal(正常),bold(加粗),italic(斜體)
android:lines 最多顯示的文本行數(shù)
android:hint 設(shè)置輸入框提示內(nèi)容(無輸入內(nèi)容時展示)
android:inputType 設(shè)置輸入類型,可選值:textPassword(密碼),number(數(shù)字)等

更多屬性及實際效果,可以在開發(fā)工具里自行體驗.


安卓開發(fā)入門教程系列匯總

開發(fā)語言學(xué)習(xí)

Kotlin語言基礎(chǔ)

UI控件學(xué)習(xí)系列

UI控件_TextView
UI控件_EditText
UI控件_Button
UI控件_ImageView
UI控件_RadioButton
UI控件_CheckBox
UI控件_ProgressBar

關(guān)注頭條號,第一時間獲取最新文章:


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容