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

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

EditText相关Blog

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>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方那座山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值