android 去掉聚焦背景,Android中EditText样式修改 聚焦光标、背景

在Android开发中,根据项目的需求,需要定制一些特殊的样式,例如:使用EditText时,聚焦时的背景及光标图片使用自定义而非android系统默认的。这两天,在项目中涉及此需求,现记录如下:

首先,说明灵感来自于http://bbs.csdn.net/topics/391491663中的评论,谢谢!另外,若想了解更多EditText属性,可参考:http://blog.csdn.net/qq_15128547/article/details/50947041

默认情况下:

android:id="@+id/editText"

android:layout_width="120dp"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:gravity="left"

android:hint="hint"

/>

默认运行效果如下:

8826cbc062ecfa1926529347754ce007.png

观察效果发现,默认情况下,聚焦背景蓝色、光标黑色、选择下方是蓝色图片(即图中的textSelectHandle)

现将上述三项改为自定义,需要下述三个属性:

android:background        背景

android:textCursorDrawable   光标

android:textSelectHandle    聚焦选择图标

设置如下:

android:id="@+id/editText"

android:layout_width="120dp"

android:layout_height="wrap_content"

android:layout_marginLeft="20dp"

android:gravity="left"

android:hint="hint"

android:background="@drawable/edit_text_bg"

android:textCursorDrawable="@drawable/edit_cursor"

android:textSelectHandle="@drawable/edit_select_handle" />

设置后运行效果如下:

f241dbbea39baf913c6ac440f0910755.png

下面,分别描述对应样式图片:

edit_text_bg:自定义背景

edit_cursor:光标

edit_select_handle:聚焦选择下方图标

上述三个,均使用自定义drawable,具体代码如下:

edit_text_bg.xml:

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

1 <?xml version="1.0" encoding="utf-8"?>

2

3 >

4

5

6

7 android:tint="@color/n_text_default_color"

8 />

9

10

11

12 android:tint="@color/n_text_default_color" />

13

14

15

16 android:tint="@color/n_text_focus_color" />

17

18

19

View Code

edit_cursor.xml:

edit_select_handle.xml:

整个修改涉及的代码如上,但需要注意一下事项:

1、若是仅某个EditText需要使用特殊样式则直接在当前EditText中设置属性即可;但为了可扩展性,建议写在styles.xml中,以后在需要的EditText中引用该样式即可,即:

...

style="@style/EditTextStyle".../>

2、若整个项目均统一风格,则在AndroidManifest.xml中的标签中,加入android:theme="@style/AppTheme",其中AppTheme中加入以下属性即可:

3、关于EditTextStyle的样式如下:

@drawable/edit_text_bg

@drawable/edit_cursor

@drawable/edit_select_handle

--------------------------补充以下,另外一张图,选择文本时,出现的图标:

1d57a4388f7a213507d54c942195c069.png

android同样提供了相应的属性,在此不再赘述:

2b65ef29a5872cc0e4771c25889edd04.gif

6a087676c59fa8b19d76e6bb55a32902.gif

android:textSelectHandleLeft="@drawable/edit_select_left"      //左

android:textSelectHandleRight="@drawable/edit_select_right"     //右

View Code

疑问:在写代码时,用到hint,但是,起初运行时,并未显示 ,多次修改也不行,最后,很之前的一个界面对比,发现当前Activity没有设置Theme,于是就设置了系统的Theme,结果就可以了,不知道问什么?知道的小伙伴,麻烦告知:

此文仅是抛砖引玉,关于EditText属性还很多,大家可以测试,有什么问题,欢迎交流!

原文:http://www.cnblogs.com/sparrowlhl/p/5780919.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值