EditText with non-selectable grayed out prefix
I'm looking for a way to have a grayed out text as prefix in an EditText. This text should be not selectable. It's a bit like the To field when you're composing a message with Gmail. The only开发者_如何学运维 (visual) difference is that this text disappears when you start typing.
Is there any trick to achieve this in Android?
Thanks!
You can use an image of the part "EUR 2500". this you can display in your editbox without affecting the rest of the part. Follow the code:
Drawable editTextDrawable = context.getResources().getDrawable(imageId);
editTextDrawable.setBounds(0, 0, editTextDrawable.getIntrinsicWidth(),
editTextDrawable.getIntrinsicHeight());
The drawable can be used inside the edittext as follows:
editTxtItemName.setCompoundDrawables(,
ListViewConstants.editTextDrawable, null, null, null);
As an ultimate solution, you can rewrite the full EditText class by extending it and modifying it in a way that it has a custom Background set by you, and a predefined padding set by you.
Put the EUR as the background, positioning it in the left side, and then give the starting padding of the EditText in such a way that the text the user types, starts right after the EUR text.
This maybe regarded as an overkill or a poor-man's solution to this problem, but still its the ultimate option. Not the smartest one perhaps, and I also don't know if its gonna work for sure :P
All the best!
精彩评论