TextInputLayout

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

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

 <android.support.design.widget.textinputlayout
      android:layout_height="wrap_content/" 
      android:layout_width="match_parent"
>
<edittext
      android:layout_height="wrap_content/"
      android:layout_width
="match_parent">

</android.support.design.widget.textinputlayout>

一定要注意,他是把EditText包含起来的,不能单独使用。

 EditText editText = textInputLayout.getEditText();
        textInputLayout.setHint(Password);
 
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public 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);
                }
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
 
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

这里需要注意的是,TextInputLayout的颜色来自style中的colorAccent的颜色:

posted @ 2016-04-08 15:27  蒋蝈蝈  阅读(557)  评论(0编辑  收藏  举报