[英]DrawableButton in EditText to Show/Hide Password and change DrawableEnd
This is the activity_register.xml
:这是
activity_register.xml
:
<EditText
android:id="@+id/editTextTextPassword"
android:layout_width="346dp"
android:layout_height="49dp"
android:layout_marginTop="32dp"
android:drawableStart="@drawable/ic_vpn_key"
android:drawableEnd="@drawable/ic_visibility"
android:ems="10"
android:hint="@string/password_hint"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextEmailAddress" />
So,所以,
I have password EditText
我有密码
EditText
When you click the drawableEnd
, you should see the password and the drawable should change to another one, which then hides the password again.当您单击
drawableEnd
时,您应该会看到密码,并且 drawable 应该更改为另一个,然后再次隐藏密码。
I found tutorials where you can show the password -> worked我找到了可以显示密码的教程->工作
_________________________change the drawable -> didn't work _________________________更改可绘制对象 -> 无效
But, I found no tutorial for a onClickListener
inside a drawable for kotlin但是,我在 kotlin 的可绘制对象中没有找到关于
onClickListener
的教程
Problem short问题短
Code to show the password if the drawable is clicked, and if it clicked another time the password hides again.如果单击可绘制对象,则显示密码的代码,如果再次单击,则密码再次隐藏。
What you want is TextInputLayout
from the Material Componenets.你想要的是来自 Material Componenets 的
TextInputLayout
。
Here's how you do it:这是你如何做到的:
Implement Material Library in your build.gradle(app) file as:在 build.gradle(app) 文件中实现材料库:
implementation 'com.google.android.material:material:1.3.0-alpha01'
Then, change the XML's EditText to然后,将 XML 的 EditText 更改为
<com.google.android.material.textfield.TextInputLayout android:id="@+id/passwordLayout" style="@style/TextInputLayoutAppearanceFilled" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/password" app:endIconMode="password_toggle" //This is used to set the password toggle behavior app:startIconDrawable="@drawable/ic_lock_black_24dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:textSize="@dimen/_12ssp" /> </com.google.android.material.textfield.TextInputLayout>
Then, Change the Style as you want in styles.xml
:然后,在
styles.xml
中根据需要更改样式:
<style name="TextInputLayoutAppearanceFilled" parent="Widget.MaterialComponents.TextInputLayout.FilledBox"> <item name="hintTextAppearance">@style/HintText</item> <item name="helperTextTextAppearance">@style/HelperText</item> <item name="android:textColor">@color/dark_grey</item> <item name="android:textColorHint">@color/color</item> <item name="hintTextColor">@color/color</item> <item name="boxStrokeColor">@color/color</item> <item name="startIconTint">@color/color</item> <item name="endIconTint">@color/color</item> <item name="boxBackgroundColor">@color/white</item> <item name="boxCornerRadiusBottomEnd">@dimen/_26sdp</item> <item name="boxCornerRadiusBottomStart">@dimen/_26sdp</item> <item name="boxCornerRadiusTopEnd">@dimen/_26sdp</item> <item name="boxCornerRadiusTopStart">@dimen/_26sdp</item> <item name="boxStrokeWidthFocused">0dp</item> <!--This destroys the visible layout in layout editor so first comment this out to design--> <!--<item name="boxStrokeWidth">0dp</item>--> <item name="hintEnabled">true</item> </style>
Here's how the output will look like: output 的外观如下:
The Password toggle will work exactly how you want it, also, the icon changes to the other one as well.密码切换将完全按照您的需要工作,而且图标也会更改为另一个。 You can further customize it as you want, have a look at the official documentation first -
TextInputLayout
可以根据需要进一步自定义,先看看官方文档
TextInputLayout
Just use the TextInputLayout
provided by the Material Components Library with the password toggle end icon:只需使用 Material Components Library 提供的
TextInputLayout
和密码切换结束图标:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
app:startIconDrawable="@drawable/..."
app:endIconMode="password_toggle"
android:hint="Password">
<com.google.android.material.textfield.TextInputLayout
android:inputType="textPassword"
.../>
</com.google.android.material.textfield.TextInputLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.