带有不可选择的灰色前缀的 EditText
我正在寻找一种方法,将灰色文本作为 EditText 中的前缀。该文本不应被选择。 它有点像使用 Gmail 撰写邮件时的“收件人”字段。唯一(视觉上)的区别是,当您开始输入时,该文本会消失。
Android 有什么技巧可以实现这一点吗?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用“EUR 2500”部分的图像。您可以将其显示在编辑框中,而不会影响该部分的其余部分。遵循代码:
可以在edittext内部使用drawable,如下所示:
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:
The drawable can be used inside the edittext as follows:
作为最终的解决方案,您可以通过扩展它并修改它来重写完整的 EditText 类,使其具有您设置的自定义背景和您设置的预定义填充。
将 EUR 作为背景,将其放置在左侧,然后给出 EditText 的起始填充,以便用户键入的文本紧接在 EUR 文本之后开始。
这可能被认为是一种矫枉过正或穷人的解决方案,但仍然是最终的选择。也许不是最聪明的,而且我也不知道它是否一定会起作用:P
祝一切顺利!
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!