Updating Android EditText's hint on focus chan

2019-08-15 01:13发布

问题:

I read with interest user sunit's answer to this question about updating an EditText's hint but have been unable to find any documentation on using the method that I presume he appears to describe there: using the <selector> element in an XML layout to dynamically adjust attributes of an EditText at runtime when the element is focused/unfocused.

In my case I am actually more interested in adjusting the android:inputType element (because the hint disappears for me when the inputType is specified) but adjusting the hint would work just as well.

To be clear I know how to make this change in Java code--I'm trying to find out if there is a way to specify the behavior in XML. Thanks!

回答1:

I'm afraid it isn't yet possible. <selector> is only valid to be applied in making state lists out of Drawable and Color resources, it does not yet work for Strings.

With regards to your mention of adjusting android:inputType to make the hint disappear, this is actually a known Android bug that will eventually be fixed in later versions so I wouldn't recommend building your code around this functionality as it will break when they fix it:

http://code.google.com/p/android/issues/detail?id=13895

Since you mentioned that you already know how to do this in the Java code, I won't point out how to call setHint() from within a OnFocusChangeListener ;)

Cheers.