TextInputLayout輸入框控件的懸浮標簽
本文實例為大家分享了TextInputLayout輸入框懸浮標簽的具體代碼,供大家參考,具體內(nèi)容如下

TextInputLayout也是5.0以后的效果,想要使用同樣需要在build中配置:
dependencies {
compile 'com.android.support:design:23.3.0'
}
TextInputLayout可以用來顯示一個提示錯誤信息,把Hint放到EditText左上方等效果的一個布局;
如果項目中有這類的需求,使用TextInputLayout實現(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="請輸入用戶名"/> </android.support.design.widget.TextInputLayout>
但是默認情況下,當你輸入文本的時候TextInputLayout只會將Hint移動到左上方,不會有錯誤提示,錯誤提示需要我們手動設置:
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
//正在輸入的時候調(diào)用
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length()>6){
//打開TextInputLayout異常提示
tilUser.setErrorEnabled(true);
//設置TextInputLayout異常提示信息
tilUser.setError("賬號最大長度為6");
}else {
//關閉TextInputLayout異常提示
tilUser.setErrorEnabled(false);
}
}
@Override
//輸入以后調(diào)用
public void afterTextChanged(Editable s) {
}
});
點擊打開鏈接免費下載源碼
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
分別用ToolBar和自定義導航欄實現(xiàn)沉浸式狀態(tài)欄
本文主要介紹了分別用ToolBar和自定義導航欄實現(xiàn)沉浸式狀態(tài)欄的方法步驟,具有一定的參考價值,下面跟著小編一起來看下吧2017-01-01
Android開發(fā)之圖形圖像與動畫(一)Paint和Canvas類學習
Paint類代表畫筆,用來描述圖形的顏色和風格,如線寬,顏色,透明度和填充效果等信息;Canvas類代表畫布,通過該類提供的構造方法,可以繪制各種圖形;感興趣的朋友可以了解下啊,希望本文對你有所幫助2013-01-01

