Android Support Design - TextInputLayout

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

注意
- TextInputLayout不能单独使用,需要包裹EditView组件。
- TextInputLayout的一些属性可以通过xml来设置,例如app:errorEnabled="true"
- hint可以设置给EditText,与TextInputLayout.setHint()设置同样效果.
- error信息只能设置给与TextInputLayout,若设置给EditText,TextInputLayout没有任何变化
- 如果使用app:errorEnabled="true"来开启error显示,EditText下方将会出现error TextView的占位。所以建议在需要显示error时使用代码开启,用完关闭.

设置hint样式

layout.xml

<android.support.design.widget.TextInputLayout
    android:id="@+id/til_name"
    app:errorEnabled="true"
    app:hintTextAppearance="@style/ErrorText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/et_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的姓名"/>
</android.support.design.widget.TextInputLayout>

style.xml

<style name="ErrorText" parent="TextAppearance.AppCompat.Small">
    <item name="android:textStyle">bold|italic</item>
    <item name="android:textColor">#00ff00</item>
</style>

设置字数统计-Counter

app:counterEnabled=”true”
app:counterMaxLength=”10”

<android.support.design.widget.TextInputLayout
    app:counterEnabled="true"
    app:counterMaxLength="10"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入您的邮箱"/>
</android.support.design.widget.TextInputLayout>

设置Error显示

til_mail= (TextInputLayout) findViewById(R.id.til_mail);
et_mail=til_mail.getEditText();
et_mail.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        //开启错误显示
        til_mail.setErrorEnabled(true);
        //设置错误显示
        til_mail.setError("Error hint,你没有权限输入。");
        til_mail.setErrorEnabled(false);

    }
});

api url:http://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值