Android进阶之路 – 设置 EditText光标颜色与下划线颜色

Android进阶之路 – 设置 EditText光标颜色与下划线颜色面对这样的需求 改变光标颜色 改变下划线颜色 面对这样的需求 我只能说 好吧 设置全局 EditText 光标颜色

面对这样的需求 :改变光标颜色 ,改变下划线颜色,面对这样的需求,我只能说…好吧…

EditText相关Blog

  • TextView、EditText属性大全
  • 监听EditText的文本变化
  • 设置EditText光标颜色与下划线颜色
  • EditText禁止输入表情符号
  • EditText一键清除内容
  • EditText首字母不可为0

Effect :

设置全局EditText光标颜色

  • 先找到 manifests
 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/>  
     intent-filter>  
      activity>  
       application> 
  • 上文中的这行代码“android:theme=“@style/AppTheme”,查看AppTheme的属性
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     
    
      "colorPrimary" 
    >@color/colorPrimary 
   
     "colorPrimaryDark" 
   >@color/colorPrimaryDark 
  
    "colorAccent">@color/colorAccent 
    
     style> 

其中

@color/colorAccent
就是设置全局的获取焦点后的光标颜色,可以修改这里变成我们需求的色值

  • 设置好之后,这就是我们的成形代码,和平常的没有区别
 <EditText android:layout_width="match_parent" android:layout_height="45dp" android:background="@null" android:hint="全局的光标颜色" android:layout_marginLeft="10dp" /> 

修改单一的某个EditText的光标颜色

主要注意 : android:textCursorDrawable="" EditText光标的一个属性,一般不太用到

  • res – drawavke – 创建对应的资源文件 -( bg_edittext )
 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">  
    <size android:width="1dp" />  
    <solid android:color="#3399dd"/>  
     shape> 
  • 在EditText中设置textCursorDrawable属性
 <EditText android:layout_width="match_parent" android:layout_height="45dp" android:background="@null" android:hint="自定义的光标颜色" android:textCursorDrawable="@drawable/bg_edittext" android:layout_marginLeft="10dp" /> 

默认的EditText展示效果

  • 原始的光标颜色与下划线颜色 ,可根据之前的全局配置进行原始展示
 <EditText android:layout_width="match_parent" android:layout_height="45dp" android:hint="默认自带下划线背景颜色" android:layout_marginLeft="10dp" /> 

设置自己喜欢的光标颜色与下划线颜色

主要注意 : android:theme=" "设置自己theme的style

  • res – values -styles 创建自己的Style样式,
  
     
    <style name="MyEditText" parent="Theme.AppCompat.Light"> 
    
      "colorControlNormal" 
    >@color/colorPrimary 
   
     "colorControlActivated" 
   >@android:color/holo_orange_dark  
     style> 
  • EditText中的使用方式,直接设置theme,如果需要替换光标颜色,可以重写textCursorDrawable属性,反之则不动
 <EditText android:layout_width="match_parent" android:layout_height="45dp" android:hint="带下划线背景颜色" android:theme="@style/MyEditText" android:layout_marginLeft="10dp" /> 

完整 Xml

 
    <LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.yongliu.cursor.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="45dp" android:background="@null" android:hint="全局的光标颜色" android:layout_marginLeft="10dp" /> <EditText android:layout_width="match_parent" android:layout_height="45dp" android:background="@null" android:hint="自定义的光标颜色" android:textCursorDrawable="@drawable/bg_edittext" android:layout_marginLeft="10dp" /> <EditText android:layout_width="match_parent" android:layout_height="45dp" android:hint="默认自带下划线背景颜色" android:layout_marginLeft="10dp" /> <EditText android:layout_width="match_parent" android:layout_height="45dp" android:hint="带下划线背景颜色" android:theme="@style/MyEditText" android:layout_marginLeft="10dp" />  
     LinearLayout> 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/198148.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月26日 下午3:41
下一篇 2026年3月26日 下午3:41


相关推荐

  • linux ip 配置及配置保存

    linux ip 配置及配置保存1配置IP#改对应网卡配置IP ifconfigeth010.120.16.82/24#配置默认路由routeadddefaultgw10.120..16.1#利用route命令配置路由如将192.168.20.X的ip路由到192.168.20.254网关routeadd-net192.168.20.0/24gw192.168.20.25

    2022年5月20日
    196
  • 谷歌离线地图开发_谷歌实时在线街景地图

    谷歌离线地图开发_谷歌实时在线街景地图离线地图开发主要有两部分组成:1、获取离线地图数据;因为离线地图一般都是局域网,所以需要离线地图数据放在内网中使用;2、离线地图服务器搭建以及二次开发接口提供,离线地图是一种服务,就像我们Apache提供的WEB服务器一样,他是一种准们的地图服务:提供了包括WEB服务、TMS服务、WMTS服务等等。离线地图数据的获取:可以通过【大地图下载器】下载到。要进…

    2026年2月3日
    10
  • OpenClaw 龙虾:腾讯云一键部署的开源智能 AI 助理

    OpenClaw 龙虾:腾讯云一键部署的开源智能 AI 助理

    2026年3月13日
    8
  • 图片管理系统–CDN源站图片管理

    这是Java开发的跨域的图片上传、图片删除解决办法;并提供了5个针对商品的图片管理功能:浏览、检查、切割、删除、上传。对于图片上传、图片删除是没有权限控制的;对于5个图片管理功能演示,使用JasigCAS进行单点登录。技术框架:SpringMVC下载连接一:图片管理系统源码下载地址:http://pan.baidu.com/s/11

    2022年4月4日
    51
  • Linux-nmap命令使用

    Linux-nmap命令使用用namp对局域网扫描一遍,然后查看arp缓存表就可以知道局域内ip-mac的对应了namp比较强大也可以直接扫描mac地址和端口进行ping扫描,打印出对扫描做出响应的主机:  nmap-sP192.168.1.0/24仅列出指定网络上的每台主机,不发送任何报文到目标主机:  nmap-sL192.168.1.0/24  探测目标主机开放的端口,可以指定一个以…

    2022年5月13日
    40
  • 【Python05】Python转义字符

    【Python05】Python转义字符1 转义字符的定义 由反斜杠加上一个字符或数字组成 它把反斜杠后面的字符或数字转换成特定的意义 2 一般转义字符有 n 代表换行符 t 代表横向跳格 代表反斜杠 代表双引号 代表单引号 r 代表回车 b 代表退格 3 题目 请各位读者在草稿纸上写写下面的输出函数应该输出什么 可以根据注释后面的提示进行思考 作者 要努力 努力 再努力 开发时间 2022 4 521 07 转义字符 print hello nworld n

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号