How to customize android passwordToggleDrawable

青春壹個敷衍的年華 提交于 2019-12-10 23:14:39

问题


Android support library added TextInputLayout adding several features like floating label and password toggle visisbilty

My problem is How to customize passwordVisisbilityDrawable so my own icons for visible and hidden state will be displayed.

Also when I enable passwordToggleVisibility, height will increase.


回答1:


for change drawable, create a selector like below

password_toggle_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_password_visible" android:state_checked="true"/>
    <item android:drawable="@drawable/ic_password_hidden"/>
</selector>

then assign to TextInputLayout like below:

<android.support.design.widget.TextInputLayout
    android:id="@+id/PasswordLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:passwordToggleEnabled="true"
    **app:passwordToggleDrawable="@drawable/password_toggle_drawable"**
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.design.widget.CommonTextInputEditText
        android:id="@+id/PasswordEditText"
        android:padding="0dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>

for height, I used below code after inflating:

CheckableImageButton text_input_password_toggle = (CheckableImageButton) PasswordLayout().findViewById(android.support.design.R.id.text_input_password_toggle);
text_input_password_toggle.setMinimumHeight(0);
PasswordEditText().setMinHeight(0);
PasswordEditText().setMinimumHeight(0);


来源:https://stackoverflow.com/questions/45397979/how-to-customize-android-passwordtoggledrawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!