Android 聚焦后更改TextInputLayout提示文本

Android 聚焦后更改TextInputLayout提示文本,android,android-fragments,android-textinputlayout,Android,Android Fragments,Android Textinputlayout,在一个片段中,我想在用户触摸TextInputInputText后更改TextInputLayout提示文本。在设置浮动提示的动画之前 我可以毫无问题地更改提示,但我希望用户先触摸它,然后再更改它 我该怎么做 谢谢您可以使用OnFocusChangedListener并检索用户拥有EditText焦点的事件 myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override

在一个片段中,我想在用户触摸TextInputInputText后更改TextInputLayout提示文本。在设置浮动提示的动画之前

我可以毫无问题地更改提示,但我希望用户先触摸它,然后再更改它

我该怎么做


谢谢

您可以使用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>
编辑:-要仅更改提示文本,只需在焦点更改时更改提示文本即可

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而不是TextInputItemText。TextInputLayout可以更改提示,但onFocusChange方法不起作用。使用此解决方案将阻止键盘显示tho',因为设置提示将隐藏键盘。
edit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus)
                edit.setHint("Enter name");
            else
                edit.setHint("Name");
        }
    });