如何在Android中更改EditText的焦点颜色
如何更改 EditText
框中的焦点颜色(橙色)?
焦点颜色是整个控件周围的小边框,并且是明亮的 当控件获得焦点时为橙色。我怎样才能改变它的颜色 聚焦到不同的颜色?
How can I change the focus color (orange) on an EditText
box?
The focus color is a small rim around the entire control and is bright
orange when the control has focus. How can I change the color of that
focus to a different color?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须创建/修改自己的 NinePatch 图像来替换默认图像,并将其用作 EditText 的背景。如果您查看平台下的 SDK 文件夹,然后查看 res/drawable,您应该会找到 EditText 焦点状态的 NinePatch 图像。如果这就是您想要更改的全部内容,您只需将其拖入 Photoshop 或您拥有的任何图像编辑软件中,然后将橙色更改为您选择的颜色即可。然后将其保存到您的可绘制文件夹中,并构建一个新的 StateListDrawable,例如如下所示:
edittext_modified_states.xml
我不知道 EditText 的默认 NinePatches 的实际名称,因此将其替换这些是必要的,但这里的关键是仅使用
@android:drawable
图像作为您尚未修改的图像(或者您可以将它们复制到项目的可绘制文件夹中),然后使用针对您的专注状态修改后的可绘制对象。然后,您可以将此 StateListDrawable 设置为 TextView 的背景,如下所示:
You'll have to create/modify your own NinePatch image to replace the default one, and use that as the background of your EditText. If you look in your SDK folder, under your platform, then res/drawable, you should find the NinePatch image for the EditText focus state. If that's all you want to change, you can just pull it into Photoshop, or whatever image editing software you have, and change the orange color to a color of your choosing. Then save that into your drawable folder, and build a new StateListDrawable, for example something like the below:
edittext_modified_states.xml
I don't know offhand the actual names for the default NinePatches for the EditText, so replace those as necessary, but the key here is to just use the
@android:drawable
images for the ones you haven't modified (or you can copy them over to your project's drawable folder), and then use your modified drawable for your focused state.You can then set this StateListDrawable as the background for your TextView, like so:
您不需要创建 xml 可绘制对象。代码上可能更简单。
科特林中的示例:
You dont need to create xml drawables. It may be more simple in code.
Example in kotlin: