Изменить цвет выбора пароля маски
![введите описание изображения здесь]()
Как вы можете видеть на картинке, у меня есть приложение для Android с черным фоном и белым текстом. Однако на самом деле есть значок "Показать текст", который выглядит как "глаз", и он также черный:( Есть ли способ изменить цвет этого?
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true"
android:background="@color/black">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<ImageView android:src="@drawable/logo"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_marginBottom="24dp"
android:layout_gravity="center_horizontal" />
<!-- Email Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColorHint="#ffffff">
<EditText android:id="@+id/input_email"
android:theme="@style/MyEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="E-Mail Address"/>
</android.support.design.widget.TextInputLayout>
<!-- Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:textColor="#ffffff"
android:textColorHint="#ffffff">
<EditText android:id="@+id/input_password"
android:theme="@style/MyEditTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="Login"/>
<TextView android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="No account yet? Create one"
android:textColor="#ffffff"
android:gravity="center"
android:textSize="16dip"/>
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<color name="bg_color">#ffffff</color>
<color name="black">#222222</color>
<style name="MyEditTextTheme">
<item name="colorControlNormal">#ffffff</item>
<item name="colorControlActivated">#ffffff</item>
<item name="colorControlHighlight">#ffffff</item>
<item name="colorAccent">@android:color/white</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textColorHint">#ffffff</item>
</style>
</resources>
Ответы
Ответ 1
Хорошо, ребята, я нашел правильный ответ, есть способ настроить его цвет.
https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#attr_android.support.design:passwordToggleTint
setPasswordVisibilityToggleTintList(ColorStateList)
Обновление: вы можете напрямую добавить следующий атрибут в TextInputLayout:
app:passwordToggleTint="#FFF"
Ответ 2
rapid3642 ответ указал в правильном направлении, но мне все еще нужно было выяснить, что именно будет работать.
Выполните следующие действия, чтобы изменить цвет вашего переключателя:
-
Создайте selector_password_visibility_toggle
в ~/res/color/
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When password is shown as text, the drawable will be off_white coloured -->
<item android:color="@color/off_white" android:state_checked="true"/>
<item android:color="@android:color/white"/>
</selector>
-
Добавьте passwordToggleTintMode
и passwordToggleTint
к вашему TextInputLayout
, как показано ниже:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleTintMode="src_atop"
app:passwordToggleTint="@color/selector_password_visibility_toggle"
app:passwordToggleEnabled="true">
Теперь ваш TextInputLayout
изменит свой цвет.
Ответ 3
Добавьте в свой макет следующее xmlns: app.
xmlns:app="http://schemas.android.com/apk/res-auto"
Теперь установите парольToggleEnabled & парольToggleTint в EditText соответственно
app:passwordToggleEnabled = "true"
app:passwordToggleTint="#FFFFFF"
Ответ 4
Вот хороший способ сделать это программно:
setPasswordVisibilityToggleTintList(AppCompatResources.getColorStateList(context, R.color.white));
Вызвать этот метод в объекте TextInputLayout.
Ответ 5
Если вы используете фон для edittext или texteditlayout, то переключение видимости пароля скрыто под фоном. Поэтому удалите фон или создайте собственный стиль и сделайте его темой для текста редактирования в формате XML.
<style name="EditText_theme" parent="">
<item name="passwordToggleTintMode">src_over</item>
</style>
Если проблема не устранена или фон не сохранен, измените тему самого приложения, так как там определены основные цвета.
Вот другой обходной путь, который не работал для меня. Это я получил от Google Android Docs,
<style name="EditText_theme" parent="">
<item name="passwordToggleTint">@color/white</item>
</style>
Ответ 6
Create a custom Style.
<style name="PasswordText" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/dt_login_text</item>
<item name="android:textColorPrimary">@color/dt_login_text</item>
</style>
XML Code. // This will change the password mask color.
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_6"
android:theme="@style/PasswordText"
android:layout_margin="@dimen/margin_3"
android:gravity="center"
android:inputType="textPassword"
android:text="Q"/>
Ответ 7
Добавить файл с рисунком selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/dim_orange_btn_pressed" />
<item android:state_focused="true" android:drawable="@color/dim_orange_btn_pressed" />
<item android:drawable="@android:color/white" />
</selector>
добавьте вашу EditText в эту строку android:background="@drawable/selector"
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="@+id/txt_pass"
android:theme="@style/MyEditTextTheme"
android:layout_width="match_parent"
android:background="@drawable/selector"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="E-Mail Address"/>
<ImageButton
android:id="@+id/btn_eye"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/eye"
/>
</LinearLayout>
В вашем коде:
buttonEye.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
txt_pass.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
txt_pass.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
};