EditText輸入框

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ù)覽圖:

EditText

上面的示例是一個(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>

案例演示:

inputType

輸入類型的值有很多咒程,上面只是舉了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ù)覽圖:

background

顏色不太清晰饥瓷,但還是有效果的剥纷。其它樣式屬性也可以嘗試,主要要為了美化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)簽添加屬性即可悠垛,效果演示如下:

selectAllOnFocus

本文簡(jiǎn)單的介紹了幾個(gè)EditText的屬性,更多關(guān)于EditText的屬性和方法可以參考官方文檔EditText娜谊。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末确买,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子纱皆,更是在濱河造成了極大的恐慌湾趾,老刑警劉巖芭商,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異搀缠,居然都是意外死亡铛楣,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門艺普,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)簸州,“玉大人,你說(shuō)我怎么就攤上這事勿侯〗陕蓿” “怎么了祭埂?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵蛆橡,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我呻拌,道長(zhǎng),這世上最難降的妖魔是什么藐握? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任猾普,我火速辦了婚禮,結(jié)果婚禮上本谜,老公的妹妹穿的比我還像新娘。我一直安慰自己溜在,他們只是感情好他托,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著培遵,像睡著了一般。 火紅的嫁衣襯著肌膚如雪籽腕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天南窗,我揣著相機(jī)與錄音郎楼,去河邊找鬼。 笑死呜袁,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的虹钮。 我是一名探鬼主播膘融,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼春畔!你這毒婦竟也來(lái)了岛都?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤线召,失蹤者是張志新(化名)和其女友劉穎多矮,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體塔逃,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年伏蚊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了躏吊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氛改。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡胜卤,死狀恐怖赁项,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情舰攒,我是刑警寧澤悔醋,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站偶芍,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜椎麦,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望琴儿。 院中可真熱鬧,春花似錦造成、人聲如沸雄嚣。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至骇吭,卻和暖如春歧寺,著一層夾襖步出監(jiān)牢的瞬間燥狰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工蛀缝, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留净当,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓俘闯,卻偏偏與公主長(zhǎng)得像忽冻,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子僧诚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容