当前位置: 代码迷 >> 综合 >> Android Design Support Library 使用详解二(TextInputLayout)
  详细解决方案

Android Design Support Library 使用详解二(TextInputLayout)

热度:96   发布时间:2023-12-18 22:30:48.0

TextInputLayout作为一个父容器控件,包装了新的EditText。通常,单独的EditText会在用户输入第一个字母之后隐藏hint提示信息,但是现在你可以使用TextInputLayout 来将EditText封装起来,提示信息会变成一个显示在EditText之上的floating label,这样用户就始终知道他们现在输入的是什么。同时,如果给EditText增加监听,还可以给它增加更多的floating label。

下面我们来看这与一个TextInputLayout:

<android.support.design.widget.TextInputLayoutandroid:id="@+id/til_pwd"android:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"/></android.support.design.widget.TextInputLayout>
一定要注意,他是把EditText包含起来的,不能单独使用。

在代码中,我们给它设置监听:

   final TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.til_pwd);EditText editText = textInputLayout.getEditText();textInputLayout.setHint("Password");editText.addTextChangedListener(new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {if (s.length() > 4) {textInputLayout.setError("Password error");textInputLayout.setErrorEnabled(true);} else {textInputLayout.setErrorEnabled(false);}}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {}});}

  相关解决方案