EditText是Android的文本輸入框,用于輸入和修改文本的用戶界面元素褐啡。EditText繼承于TextView,所以TextView上的大部分方法和屬性也適用于EditText芝加。
使用EditText
使用輸入框特別簡(jiǎn)單硅卢,只需要在布局中加入<EditText/>
標(biāo)簽即可,其中寬高屬性是必須的将塑。另外id、hint点寥、inputType等屬性也建議要有。
android:id
:設(shè)置控件的id 用于java代碼中找到該控件android:layout_width
: 控件的寬度 常用值 match_parent(填充父容器) wrap_content(包裹內(nèi)容)android:layout_height
: 控件的高度 常用值 match_parent(填充父容器) wrap_content(包裹內(nèi)容)android:hint
:輸入框的提示文本(大家最常見的 請(qǐng)輸入關(guān)鍵字...)android:textColorHint
:提示文本的顏色android:autofillHints
:針對(duì)自動(dòng)填充提供服務(wù)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入用戶名"
android:inputType="text"
android:autofillHints="username"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
效果預(yù)覽圖:
上面的示例是一個(gè)居中的EditText輸入框敢辩,默認(rèn)樣式為一個(gè)帶下劃線的文本蔽莱,點(diǎn)擊輸入框獲取焦點(diǎn)后會(huì)自動(dòng)彈出輸入法鍵盤。
輸入類型
EditText設(shè)置輸入類型(inputType屬性)可以驗(yàn)證部分?jǐn)?shù)據(jù)戚长,系統(tǒng)可以根據(jù)輸入類型彈出合適的輸入框盗冷,比如數(shù)字、密碼同廉、常規(guī)文本等仪糖。
-
android:inputType
:輸入框輸入的內(nèi)容(文本、數(shù)字迫肖、密碼等)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請(qǐng)輸入用戶名锅劝,字母或數(shù)字"
android:inputType="text|number"
android:autofillHints="username"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="password"
android:hint="請(qǐng)輸入密碼,僅數(shù)字"
android:inputType="numberPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
案例演示:
輸入類型的值有很多咒程,上面只是舉了text和numberPassword兩個(gè)例子鸠天。
美化EditText
默認(rèn)的EditText樣式實(shí)在是太不怎么樣,我們可以通過(guò)設(shè)置background屬性為drawable資源進(jìn)行EditText樣式美化帐姻。
android:background
:控件背景顏色稠集、圖像android:backgroundTint
:控件背景色調(diào)android:layout_marginXx
:控件外邊距android:paddingXx
:控件內(nèi)邊距
用一下前面用過(guò)的邊框drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 實(shí)心 -->
<solid android:color="#FFFFFF"/>
<!-- 邊框 -->
<stroke
android:width="1dp"
android:color="#FF00FF"/>
<!-- 圓角 -->
<corners android:radius="3dp"/>
<!-- 邊距 -->
<padding
android:top="2dp"
android:bottom="2dp"
android:left="6dp"
android:right="6dp"/>
</shape>
EditText的background屬性值設(shè)置為上面的drawable文件就實(shí)現(xiàn)了邊框背景。
效果預(yù)覽圖:
顏色不太清晰饥瓷,但還是有效果的剥纷。其它樣式屬性也可以嘗試,主要要為了美化EditText呢铆,怎么覺(jué)得好看怎么來(lái)晦鞋。
EditText其它屬性
控件EditText屬性很多,除了以上的棺克,還經(jīng)常用到以下一些屬性:
android:selectAllOnFocus
:獲得焦點(diǎn)全選android:gravity
:文本對(duì)齊方式android:minLines="2"
:最少行數(shù)(文本不夠會(huì)垂直居中顯示)android:maxLines="3"
:最多行數(shù)(文本超出會(huì)產(chǎn)生縱向滾動(dòng))
直接在EditText標(biāo)簽添加屬性即可悠垛,效果演示如下:
本文簡(jiǎn)單的介紹了幾個(gè)EditText的屬性,更多關(guān)于EditText的屬性和方法可以參考官方文檔EditText娜谊。