面对这样的需求 :改变光标颜色 ,改变下划线颜色,面对这样的需求,我只能说…好吧…
EditText相关Blog
- TextView、EditText属性大全
- 监听EditText的文本变化
- 设置EditText光标颜色与下划线颜色
- EditText禁止输入表情符号
- EditText一键清除内容
- EditText首字母不可为0
Effect :
[外链图片转存中…(img-rknphmSY-1569574773087)]
设置全局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">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
其中 <item name="colorAccent">@color/colorAccent</item>
就是设置全局的获取焦点后的光标颜色,可以修改这里变成我们需求的色值
- 设置好之后,这就是我们的成形代码,和平常的没有区别
<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 )
<?xml version="1.0" encoding="utf-8"?>
<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样式,
<!--colorControlNormal 为没有获取焦点时候下划线的颜色-->
<!--colorControlActivated 为获取焦点时候光标与下划线的颜色-->
<style name="MyEditText" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/colorPrimary</item>
<item name="colorControlActivated">@android:color/holo_orange_dark</item>
</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
<?xml version="1.0" encoding="utf-8"?>
<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>