我想在android edittext中更改/显示show password的不同图标。 我正在使用以下代码来显示图标。
<android.support.design.widget.TextInputLayout
android:id="@+id/layoutTextInput"
android:textColorHint="@color/aluminium">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/editTextValue"
android:imeOptions="actionNext"
android:layout_marginBottom="8dp"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
我想使用自定义图标而不是普通图标(眼睛图标)。请帮帮我。
答案 0 :(得分:14)
创建一个新的可绘制文件并将其命名为 show_password_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_visibility_black_18dp" android:state_checked="true"/>
<item android:drawable="@drawable/ic_visibility_off_black_18dp"/>
</selector>
并在您的布局文件中,在app:passwordToggleDrawable
TextInputLayout
属性
<android.support.design.widget.TextInputLayout
android:id="@+id/layoutTextInput"
app:passwordToggleDrawable="@drawable/show_password_selector"
android:textColorHint="@color/gray">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/editTextValue"
android:imeOptions="actionNext"
android:layout_marginBottom="8dp"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
答案 1 :(得分:4)
使用app:passwordToggleDrawable
更改图标。
使用app:passwordToggleTint
更改图标的颜色,这仅在图标是矢量可绘制时才有效。
<android.support.design.widget.TextInputLayout
android:id="@+id/layoutTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/colorPrimary"
app:passwordToggleDrawable="@drawable/ic_visibility_on">
<android.support.design.widget.TextInputEditText
android:id="@+id/editTextValue"
android:layout_width="match_parent"
android:layout_height="60dp"
android:drawablePadding="5dp"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
答案 2 :(得分:2)
如果您想使用默认的眼睛图标(显示/隐藏密码)但要更改图标颜色 然后只需将线放在
app:passwordToggleTint="@color/yourColor"
如果要使用自定义眼睛图标,则应使用
app:passwordToggleDrawable
更改图标。并使用
app:passwordToggleTint
更改图标的颜色。您的自定义图标颜色不显示。将显示色调颜色。整个xml代码如下:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/yourColor"
android:theme="@style/TextLabelLogin"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/yourColor"
app:passwordToggleDrawable="@drawable/show_password_selector">
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_line_shape"
android:hint="@string/password"
android:textColorHint="@color/yourColor"
android:inputType="textPassword"
android:textColor="@color/yourColor"/>
</android.support.design.widget.TextInputLayout>
和show_password_selector.xml如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_hide_password" android:state_checked="true" />
<item android:drawable="@drawable/ic_show_password" /></selector>
希望对所有人有帮助。
答案 3 :(得分:1)
**Add Dependency**
compile 'com.android.support:design:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:support-vector-drawable:25.0.1'
<android.support.design.widget.TextInputLayout
android:id="@+id/layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/layout_email"
android:textColorHint="@color/colorHint"
应用:passwordToggleEnabled =&#34;真&#34;&GT; 强>
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:textColor="@color/textColor" />
</android.support.design.widget.TextInputLayout>
答案 4 :(得分:0)
在您的Xml中使用TextInputLayout
和Imageview
<RelativeLayout
android:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeone"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal"
android:weightSum="5">
<android.support.design.widget.TextInputLayout>
android:id="@+id/layoutTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_vertical"
android:layout_weight="5"
android:gravity="center_vertical"
android:hint="Password"
android:paddingTop="4dp"
android:textColorHint="#3f3f3f">
<EditText
android:id="@+id/passwordedit"
android:layout_width="match_parent"
android:layout_height="60dp"
android:drawableLeft="@mipmap/bluelocked"
android:drawablePadding="13dp"
android:gravity="center_vertical"
android:inputType="textPassword"
android:paddingLeft="15dp"
android:textColor="#3f3f3f"
android:textColorHint="#3f3f3f"
android:textSize="13sp" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/imagepassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:paddingBottom="20dp"
android:paddingRight="10dp"
android:src="@mipmap/IMAGEYOUWANT" />
</RelativeLayout>
在您的活动中添加onTouch()
方法,以显示和隐藏您的密码。
ImageView imagepass = (ImageView) findViewById(R.id.imagepassword);
imagepass .setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
editpass.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editpass.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});
答案 5 :(得分:0)
我使用文本实现它..即“显示”和“隐藏”
textViewShowPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (textViewShowPassword.getText().toString().equalsIgnoreCase("Show")) {
editTextPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
textViewShowPassword.setText("Hide");
editTextPassword.setSelection(editTextPassword.getText().length());
} else {
textViewShowPassword.setText("Show");
editTextPassword.setTransformationMethod(new PasswordTransformationMethod());
editTextPassword.setSelection(editTextPassword.getText().length());
}
}
});
希望对你有所帮助.. !!
答案 6 :(得分:0)
1.create a xml layout...I have created in my way
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_50dp"
android:layout_marginTop="@dimen/_15dp">
<android.support.v7.widget.AppCompatEditText
android:id="@+id/etCurrentPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/drawable_edit_text_2"
android:fontFamily="@font/poppins"
android:hint="@string/old_password"
android:inputType="textPassword"
android:padding="@dimen/_10dp" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/imgShowPassword1"
android:layout_width="@dimen/_50dp"
android:layout_height="@dimen/_50dp"
android:layout_gravity="end|center_vertical"
android:layout_marginRight="@dimen/_10sp"
android:padding="@dimen/_12dp"
android:tint="@android:color/darker_gray"
app:srcCompat="@drawable/ic_eye" />
</FrameLayout>
2.In activity class: call the method with the mathing parameters
showORhidePass(view,etRetypePassword,showPassword3 = !showPassword3);
void showORhidePass(AppCompatImageView imageView, AppCompatEditText editText, Boolean save){
if (TextUtils.isEmpty(editText.getText())){
return;
}
if (save){
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
editText.setSelection(editText.length());
imageView.setColorFilter(ContextCompat.getColor(requireContext(), android.R.color.darker_gray));
}else{
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
editText.setSelection(editText.length());
imageView.setColorFilter(ContextCompat.getColor(requireContext(), R.color.colorPrimary));
}
}
答案 7 :(得分:0)
使用AndroidX和Android Material,您只需-> app:passwordToggleEnabled =“ true”
` <com.google.android.material.textfield.TextInputLayout
android:id="@+id/TextInputLayout_password"
style="@style/TextInputLayoutStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:hint="Password"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/password_EditText"
style="@style/EditText_Common_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:imeOptions="actionDone"
android:inputType="textPassword"
app:errorEnabled="true" />
</com.google.android.material.textfield.TextInputLayout>`
答案 8 :(得分:0)
使用Material Components库,只需使用 app:endIconDrawable
属性:
<com.google.android.material.textfield.TextInputLayout
app:endIconMode="password_toggle"
app:endIconDrawable="@drawable/...."
..>
<com.google.android.material.textfield.TextInputEditText
../>
</com.google.android.material.textfield.TextInputLayout>