天天看點

Android 5.0+ 解析(六)TextInputLayout控件

hello,大家好!

今天給大家更新的部落格是有關TextInputLayout的簡單介紹。

TextInputLayout簡介

TextInputLayout是把EditText作為自己子控件的一個布局,當輸入文字時,它可以把Hint文字飄到EditText的上方。它解決了當使用者點選EditText時Hint的文字消失了而導緻使用者可能不知道目前輸入的内容是什麼的問題。

TextInputLayout使用

1.導入依賴包

<span style="font-size:18px;">dependencies {
  compile 'com.android.support:appcompat-v7:22.2.0'
  compile 'com.android.support:design:22.2.0'
}</span>
           

2.布局檔案

<span style="font-size:18px;"><android.support.design.widget.TextInputLayout
          android:layout_width="fill_parent"
          android:id="@+id/til_hint_content"
          app:errorEnabled="true"
          android:layout_height="wrap_content">

        <EditText android:id="@+id/et_hint_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:saveEnabled="false"
            android:maxLength="48"
            android:digits="1234567890qwertyuiopasdfghjklzxcvbnm "
            android:hint="你的提示文字"/>
</android.support.design.widget.TextInputLayout></span>
           

TextInputLayout添加邏輯判斷

<span style="font-size:18px;">et_hint_content.addTextChangedListener(new TextWatcher() {
      @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      //輸入不符合設定的字元,hint會變成紅色并提醒
      @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        //檢查輸入是否符合自定義規則
        if (checkType(charSequence.toString())) {
          til_hint_content.setErrorEnabled(true);
          til_hint_content.setError("輸入資訊有誤");
          return;
        } else {
          til_hint_content.setErrorEnabled(false);
        }
      }

      @Override public void afterTextChanged(Editable editable) {

      }
    });</span>
           

歡迎留言和評論!

部落格位址:http://blog.csdn.net/caihongdao123 

下一篇  Android 5.0+ 解析(七)Snackbar控件

繼續閱讀