Изменить текст подсказки TextInputLayout после фокусировки

Во фрагменте я хотел бы изменить текст подсказки TextInputLayout после того, как пользователь коснется TextInputEditText. Перед тем, как он анимируется до плавающей подсказки.

Я могу изменить подсказку без проблем, но я хочу, чтобы пользователь сначала прикоснулся к ней, а затем изменил ее.

Как я могу это сделать?

Спасибо

2
0
3 707
3
Перейти к ответу Данный вопрос помечен как решенный

Ответы 3

Вы можете использовать OnFocusChangedListener и получить событие, которое пользователь имеет фокус EditText.

myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                     // do what you have to do
                }

            }
        });

Попробуйте так ..

     editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean b) {
            editText.setHint("Please Enter Name"); // here set new hint
        }
    });
Ответ принят как подходящий

Чтобы изменить цвет текста подсказки Используйте View.OnFocusChangeListener для настройки hintTextAppearance. Попробуйте конфигурацию ниже.

 <style name = "inactive" parent = "Theme.AppCompat.Light">
    <item name = "android:textColorPrimary">@android:color/darker_gray</item>
    <item name = "android:textColor">@android:color/darker_gray</item>
</style>

<style name = "active" parent = "Theme.AppCompat.Light">
    <item name = "android:textColorPrimary">@android:color/black</item>
    <item name = "android:textColor">@android:color/black</item>
</style>

XMl

<android.support.design.widget.TextInputLayout
    android:id = "@+id/tet"
    android:layout_width = "match_parent"
    android:layout_height = "wrap_content"
    android:layout_below = "@+id/et"
    app:hintTextAppearance = "@style/inactive"
    android:layout_margin = "@dimen/activity_horizontal_margin"
    app:hintEnabled = "true">
    <android.support.design.widget.TextInputEditText
        android:id = "@+id/edit"
        android:layout_width = "match_parent"
        android:textColor = "@color/colorPrimary"
        android:layout_height = "wrap_content"
        android:hint = "Floating Hint" />

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

Измените стиль во время смены фокуса.

final TextInputLayout inputLayout=findViewById(R.id.tet);
    final TextInputEditText edit=findViewById(R.id.edit);
    edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                inputLayout.setHintTextAppearance(R.style.active);
            else
                inputLayout.setHintTextAppearance(R.style.inactive);
        }
    });

РЕДАКТИРОВАТЬ: - Чтобы изменить только текст подсказки, вы можете просто изменить его при изменении фокуса.

edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)
                edit.setHint("Enter name");
            else
                edit.setHint("Name");
        }
    });

Спасибо. Моя проблема заключалась в том, что я использовал TextInputLayout вместо TextInputEditText. TextInputLayout мог изменить подсказку, но метод onFocusChange не работал.

Ryan 20.05.2018 00:48

использование этого решения не позволит клавиатуре отображать tho ', поскольку установка подсказки скроет клавиатуру.

Stack Diego 15.06.2021 09:42

Другие вопросы по теме