TextInputLayout輸入框控件的懸浮標(biāo)簽
本文實(shí)例為大家分享了TextInputLayout輸入框懸浮標(biāo)簽的具體代碼,供大家參考,具體內(nèi)容如下
TextInputLayout也是5.0以后的效果,想要使用同樣需要在build中配置:
dependencies { compile 'com.android.support:design:23.3.0' }
TextInputLayout可以用來顯示一個(gè)提示錯(cuò)誤信息,把Hint放到EditText左上方等效果的一個(gè)布局;
如果項(xiàng)目中有這類的需求,使用TextInputLayout實(shí)現(xiàn)起來非常方便;
使用方法也比較簡單,直接用TextInputLayout包裹EditText即可:
<android.support.design.widget.TextInputLayout android:id="@+id/til_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp"> <EditText android:id="@+id/et_user" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入用戶名"/> </android.support.design.widget.TextInputLayout>
但是默認(rèn)情況下,當(dāng)你輸入文本的時(shí)候TextInputLayout只會(huì)將Hint移動(dòng)到左上方,不會(huì)有錯(cuò)誤提示,錯(cuò)誤提示需要我們手動(dòng)設(shè)置:
etUser= (EditText) findViewById(R.id.et_user); tilUser= (TextInputLayout) findViewById(R.id.til_user); //添加文本變化監(jiān)聽 etUser.addTextChangedListener(new TextWatcher() { @Override //輸入文本之前調(diào)用 public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override //正在輸入的時(shí)候調(diào)用 public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.length()>6){ //打開TextInputLayout異常提示 tilUser.setErrorEnabled(true); //設(shè)置TextInputLayout異常提示信息 tilUser.setError("賬號(hào)最大長度為6"); }else { //關(guān)閉TextInputLayout異常提示 tilUser.setErrorEnabled(false); } } @Override //輸入以后調(diào)用 public void afterTextChanged(Editable s) { } });
點(diǎn)擊打開鏈接免費(fèi)下載源碼
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Android Studio安裝ButterKnife插件(手動(dòng)安裝)
這篇文章主要介紹了詳解AndroidStudio安裝ButterKnife插件(手動(dòng)安裝),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08Android中監(jiān)聽軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼
這篇文章主要介紹了Android中監(jiān)聽軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼,本文直接給出核心實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04Android Studio如何為Activity添加自定義注解信息
好久沒用寫文章了,今天給大家分享Android Studio如何為Activity添加自定義注解信息,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-06-06android實(shí)現(xiàn)自動(dòng)滾動(dòng)的Gallary控件效果
這篇文章主要介紹了android實(shí)現(xiàn)自動(dòng)滾動(dòng)的Gallary控件效果,涉及Android中Gallary控件的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Android開發(fā)中應(yīng)用程序分享功能實(shí)例
這篇文章主要介紹了Android開發(fā)中應(yīng)用程序分享功能,結(jié)合實(shí)例形式分析了基于Intent實(shí)現(xiàn)Android程序分享功能的技巧,需要的朋友可以參考下2016-02-02Flutter 自定義Drawer 滑出位置的大小實(shí)例代碼詳解
這篇文章主要介紹了Flutter 自定義Drawer 滑出位置的大小,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Android實(shí)現(xiàn)自定義dialog的代碼
這篇文章主要介紹了Android實(shí)現(xiàn)自定義dialog的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11Android修行手冊(cè)之ConstraintLayout布局使用詳解
這篇文章主要為大家介紹了Android修行手冊(cè)之ConstraintLayout使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09