TextInputLayout 使用中遇到的问题

序:
今天准备写一个登陆Demo,用到了TextInputLayout ,心血来潮用了一下counter、passwordToggle,记录一下使用过程中遇到的问题。
开发环境: win7 + AS 3.0.1


TextInputLayout 使用前准备

添加库依赖

要使用这个控件,需要引入appcompat-v7以及Design Support Library两个库,
根据需要在app的build.gradle 中引入。

dependencies {
    compile 'com.android.support:appcompat-v7:xx.xx.xx'
    compile 'com.android.support:design:xx.xx.xx'
}

Counter 部分

问题一 counter 开启

counter 默认是关闭的,counterEnabled 可以在xml中控制开启

<android.support.design.widget.TextInputLayout
        android:id="@+id/login_user_text_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/login_logo_iv"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:textColorHint="@android:color/white"

        app:counterEnabled="true"         
        app:counterMaxLength="11"         
        app:counterTextAppearance="@style/CounterStyle"   
        app:counterOverflowTextAppearance="@style/CounterOverflowStyle" 
        >

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/select_bg_line"
            android:inputType="number"
            android:hint="手机号"
            android:padding="5dp"
            android:textColor="@android:color/white" />

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

        <!-- counterEnabled ="true" 表示开启
        app:counterEnabled="true"

        计数器的最大长度  
        app:counterMaxLength="11"

        计数器正常状态样式 
        app:counterTextAppearance="@style/CounterStyle"  

        计数器越位状态样式 
        app:counterOverflowTextAppearance="@style/CounterOverflowStyle"
        -->
问题二 counter 的样式

counter 正常状态的样式、越位状态的样式分别使用counterTextAppearance、counterOverflowTextAppearance 表示,

app:counterTextAppearance="@style/CounterStyle"
app:counterOverflowTextAppearance="@style/CounterOverflowStyle"

可以设置字体颜色 和 字体大小

CounterStyle
    <style name="CounterStyle" >
        <item name="android:textColor">@color/colorAccent</item>
        <item name="android:textSize">12sp</item> 
    </style>

passwordToggle

问题三 passwordToggle 自定义图标

感慨一下,这个地方可是费了一番功夫,因为用了一张图片的不同颜色,我以为是drawable select 没起作用,各种查,结果并不是,是因为passwordToggle 图标 和颜色分离(猜测,没看源码)

 <android.support.design.widget.TextInputLayout
        android:id="@+id/login_pwd_text_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/login_user_text_input"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:textColorHint="@android:color/white"

        app:passwordToggleEnabled="true" 
        app:passwordToggleDrawable="@drawable/select_pwd_visible" 
        app:passwordToggleTint="@color/select_pwd_visible" 
        >

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/select_bg_line"
            android:inputType="textPassword"
            android:hint="密码"
            android:padding="5dp"
            android:textColor="@android:color/white" />

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

        <!--  
        开启 passwordToggle
        app:passwordToggleEnabled="true"

        Drawable select  
        app:passwordToggleDrawable="@drawable/select_pwd_visible"

        Color select 
        app:passwordToggleTint="@color/select_pwd_visible"
         -->

如上,

替换图标 需要 passwordToggleDrawable 和 passwordToggleTint 配合使用
select_pwd_visible.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pwd_visible_yes" android:state_checked="true"></item>
    <item android:drawable="@drawable/pwd_visible_no" android:state_checked="false"></item>
</selector>
select_pwd_visible.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent" android:state_checked="true"></item>
    <item android:color="@color/colorAccent" android:state_checked="false"></item>
</selector>

根据需要编写 select

注:

1.位置, counterEnabled、passwordToggleEnabled等属性位于TextInputLayout 内,放在TextInputEditText 内 ,无效且编译通过

2.眼睛(passwordToggleEnabled=”true” 默认是眼睛),须配合 EditText inputType=”textPassword”等密文type。这点是必然的,不是密文也用不着passwordToggle ,无效且编译通过,不显示眼睛,不影响密文输入

3.前缀app:counterEnabled,根据自己的定义使用,:编译不通过

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值