Android如何自定义EditText光标与下划线颜色详解

Android如何自定义EditText光标与下划线颜色详解,第1张

概述前言最近在写些小Demo复习基础,在用到EditText的时候突然发现之前几乎没有注意到它的光标下划线的颜色,于是花了不少时间,看了不少博客,现在就来总结和分享一下收获,话不多说了,来一起看看详细的介绍:

前言

最近在写些小Demo复习基础,在用到EditText的时候突然发现之前几乎没有注意到它的光标和下划线的颜色,于是花了不少时间,看了不少博客,现在就来总结和分享一下收获,话不多说了,来一起看看详细的介绍:

1、第一印象:原生的EditText

我们要在原生的EditText上修改,首先当然要认识一下它的本来面目。在AndroID Studio中新建一个工程,让MainActivity继承于AppCompatActivity(为什么要这样做,后面再说),然后在MainActivity的布局中放置一个EditText:

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:orIEntation="vertical" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.lindroID.edittext.MainActivity"> <EditText androID:hint="原生的EditText" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> </linearLayout>

运行工程,仔细观察可以看到光标和下划线都是粉红色的。现在就让我们循序渐进,先修改它的光标颜色。


2、自定义光标颜色

EditText 有一个属性:androID:textCursorDrawable ,它就是用来设置光标样式的。为了加深认识,大家先额外做个小实验:将textCursorDrawable设置为@null,表示去除系统默认的样式,但我们都记得隐藏光标的属性是androID:cursorVisible ,那么这时光标会是什么样子的呢?你可以给文字(androID:textcolor)和提示文字(androID:textcolorHint属性)设置不同的颜色,运行之后就会发现此时光标的颜色是跟文字的保持一致的。

了解了androID:textCursorDrawable 的作用之后,我们可以在drawable资源文件夹下新建一个cursor_color.xml文件,内容如下

<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:shape="rectangle"> <size androID:wIDth="2dp" /> <solID androID:color="@androID:color/holo_blue_light" /></shape>

光标的颜色为系统自带的浅蓝色,宽度为2dp。在原生的EditText下面放置一个新的EditText:

 <EditText androID:textCursorDrawable="@drawable/cursor_color" androID:hint="自定义光标颜色" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" />

运行效果如下:


3、取消背景后的EditText

第2节中,我们将属性androID:textCursorDrawable 设置为“@null”之后发现光标的样式会变得跟文字的颜色一样,那么如果将整个EditText的背景设置为“@null”呢?我们可以添加一个EditText,然后为它增加属性androID:background="@null" 

可以看到,虽然光标的样式没有改变,但是下划线消失了,不过除此之外,EditText的边距也没有了,如果不是光标在闪烁,一眼看上去就像个TextVIEw了。

网上有些自定义EditText下划线的教程就是这样 *** 作的,先把背景去除,再在下面加一个横线。这样的 *** 作未尝不可,但是为了美观,还是得重新设置间距值。。

4、自定义主题修改下划线

还记得刚才我们在创建MainActivity时要继承AppCompatActivity吗?到了这里就要揭晓答案了。这样做是为了使用appcompat-v7包中的Material Design样式,比如我们可以在Styles.xml文件中新建一个MyEditText样式:

 <style name="MyEditText" parent="theme.AppCompat.light">  <item name="colorControlnormal">@androID:color/darker_gray</item>  <item name="colorControlActivated">@androID:color/holo_orange_dark</item> </style>

colorControlnormal 表示控件默认的颜色,colorControlActivated 表示控件被激活时的颜色,这样,我们就可以分别设置EditText不被选中和选中时的颜色了。这里我将选中的颜色设为橙色。

在activity_main.xml中再增加一个EditText,加上androID:theme="@style/MyEditText" 属性,效果如下:

可以看到,光标和下划线的颜色都会修改掉,而间距还是会保留。

5、全局修改EditText颜色

前面的做法都是针对一个EditText来修改的,如果需要把项目中所有的EditText的颜色都改掉的话,那这样做的话工作量就太大了。有没有办法可以一脚定江山的呢?

不知道你发现了没有,为什么EditText默认是骚气的粉红色呢?事实上,你设置其他几种控件(比如Progressbar、Switch等等),它们的颜色基本上也是骚粉。你只要再看一眼刚才的styles.xml,里面的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>

看到了吗?里面的colorAccent就是那个骚粉色了。为了理解这三种颜色,我特地找了一张图:


6、继承Activity时自定义下划线

前面我们做的自定义下划线 *** 作都是在继承AppCompatActivity的前提下,如果你改成Activity,然后在AndroID5.0以下的手机运行的话,效果是这样的:


Material Design风格消失了,光标的颜色虽然还能修改,但是下划线的颜色却改不了。所以我们还得另想方法。

EditText是一个输入框,我们可以这样理解:下划线无非就是给输入框的下边框加一条线。这个用AndroID中的@R_908_3419@(图层)就可以做到。新建两个xml文件:et_underline_unselected.xml和et_underline_selected.xml,前者是EditText被选中时的背景,后者则是未被选中时的背景:

et_underline_unselected.xml

<?xml version="1.0" enCoding="utf-8"?><@R_908_3419@ xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item  androID:bottom="0dp"  androID:left="-2dp"  androID:right="-2dp"  androID:top="-2dp">  <shape>   <solID androID:color="@androID:color/transparent" />   <stroke    androID:wIDth="1dp"    androID:color="@androID:color/darker_gray" />   <padding androID:bottom="4dp" />  </shape> </item></@R_908_3419@>

et_underline_selected.xml

<?xml version="1.0" enCoding="utf-8"?><@R_908_3419@ xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item  androID:bottom="0dp"  androID:left="-2dp"  androID:right="-2dp"  androID:top="-2dp">  <shape>   <solID androID:color="@androID:color/transparent" />   <stroke    androID:color="@androID:color/holo_green_light"    androID:wIDth="2dp" />   <padding androID:bottom="4dp" />  </shape> </item></@R_908_3419@>

我将@R_908_3419@理解成一个图层列表,shape就是列表中的一个item,由于我们只需要下边框有横线,所以除了shape在列表中的下边距外都设为负值。光标和下划线之间要有点距离,所以shape的下方内边距设为4dp。当然,被选中时的下划线宽度要大一点。

在项目中新建一个SecondActivity,继承于Activity,然后在布局文件中放置两个EditText,background都设为“@null”,光标就用我们之前的浅蓝色。

<linearLayout 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" androID:orIEntation="vertical" tools:context="com.lindroID.edittext.SecondActivity"> <EditText  androID:ID="@+ID/editText1"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:layout_margin="3dp"  androID:background="@null"  androID:hint="自定义EditText下划线1"  androID:textCursorDrawable="@drawable/cursor_color" /> <EditText  androID:ID="@+ID/editText2"  androID:layout_wIDth="match_parent"  androID:layout_height="wrap_content"  androID:layout_margin="3dp"  androID:background="@null"  androID:hint="自定义EditText下划线2"  androID:textCursorDrawable="@drawable/cursor_color" /></linearLayout>

然后在代码中设置EditText的监听事件

  /**初始化EditText,默认都为未选中状态**/  editText1.setBackgroundResource(R.drawable.et_underline_unselected);  editText2.setBackgroundResource(R.drawable.et_underline_unselected);  /**第一个EditText的焦点监听事件**/  editText1.setonFocuschangelistener(new VIEw.OnFocuschangelistener() {   @OverrIDe   public voID onFocusChange(VIEw v,boolean hasFocus) {    if (hasFocus) {     Log.e(TAG,"EditText1获得焦点");     editText1.setBackgroundResource(R.drawable.et_underline_selected);    } else {     Log.e(TAG,"EditText1失去焦点");     editText1.setBackgroundResource(R.drawable.et_underline_unselected);    }   }  });  /**第二个EditText的焦点监听事件**/  editText2.setonFocuschangelistener(new VIEw.OnFocuschangelistener() {   @OverrIDe   public voID onFocusChange(VIEw v,"EditText2获得焦点");     editText2.setBackgroundResource(R.drawable.et_underline_selected);    } else {     Log.e(TAG,"EditText2失去焦点");     editText2.setBackgroundResource(R.drawable.et_underline_unselected);    }   }  });

注意:要先将所有的EditText都设置为运行一下,效果如下:


效果我们是实现了,但是这样一来Activity中的代码显得太冗长,因此我们可以将选中和未选中的状态封装到状态选择器中。在drawable文件夹下新建一个et_underline_selector.xml文件:

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID"> <item androID:state_focused="false" androID:drawable="@drawable/et_underline_unselected"/> <item androID:state_focused="true" androID:drawable="@drawable/et_underline_selected"/></selector>

androID:state_focused表示控件是否获得焦点。然后在布局文件中设置

androID:background="@drawable/et_underline_selector" ,Activity的焦点监听代码删去就可以了。运行,就可以看到一模一样的效果了。

7、后记

文章至此就结束了,但是我要学的东西还有很多,文章里的某些知识出于我个人理解,可能会有不足或者错误,欢迎大家指正!

由于这里的代码比较简单,工程就不上传了,大家动手敲一敲,相信没有问题的。

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。

参考文献

Android EditText 改变边框颜色

Android更改EditText下划线颜色样式的方法

总结

以上是内存溢出为你收集整理的Android如何自定义EditText光标与下划线颜色详解全部内容,希望文章能够帮你解决Android如何自定义EditText光标与下划线颜色详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1144796.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存