溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

TextInputLayout輸入框控件懸浮標(biāo)簽的示例分析

發(fā)布時(shí)間:2021-07-19 09:54:47 來(lái)源:億速云 閱讀:192 作者:小新 欄目:移動(dòng)開發(fā)

小編給大家分享一下TextInputLayout輸入框控件懸浮標(biāo)簽的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

具體內(nèi)容如下

TextInputLayout輸入框控件懸浮標(biāo)簽的示例分析

TextInputLayout也是5.0以后的效果,想要使用同樣需要在build中配置:

dependencies { 
 compile 'com.android.support:design:23.3.0' 
}

TextInputLayout可以用來(lái)顯示一個(gè)提示錯(cuò)誤信息,把Hint放到EditText左上方等效果的一個(gè)布局;
如果項(xiàng)目中有這類的需求,使用TextInputLayout實(shí)現(xiàn)起來(lái)非常方便;
使用方法也比較簡(jiǎ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)最大長(zhǎng)度為6"); 
  }else { 
   //關(guān)閉TextInputLayout異常提示 
   tilUser.setErrorEnabled(false); 
  } 
  } 
 
  @Override 
  //輸入以后調(diào)用 
  public void afterTextChanged(Editable s) { 
  } 
 });

以上是“TextInputLayout輸入框控件懸浮標(biāo)簽的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI