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

来源:互联网 发布:批发零售软件 编辑:程序博客网 时间:2024/06/06 02:02

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

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

<android.support.design.widget.TextInputLayout        android:id="@+id/til_pwd"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <EditText            android: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() {            @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) {            }        });    }

0 0