本文翻译自:Set EditText cursor color

I am having this issue where I am using the Android's Holo theme on a tablet project. 我有这个问题,我在平板电脑项目中使用Android的Holo主题。 However, I have a fragment on screen which has a white background. 但是,我在屏幕上有一个具有白色背景的片段。 I am adding an EditText component on this fragment. 我在这个片段上添加了一个EditText组件。 I've tried to override the theme by setting the background of the Holo.Light theme resources. 我试图通过设置Holo.Light主题资源的背景来覆盖主题。 However, my text cursor (carat) remains white and hence, invisible on screen (I can spot it faintly in the edittext field..). 但是,我的文本光标(克拉)仍然是白色的,因此在屏幕上看不见(我可以在编辑文本字段中隐约发现它...)。

Does anyone know how I can get EditText to use a darker cursor color? 有谁知道如何让EditText使用更暗的光标颜色? I've tried setting the style of the EditText to "@android:style/Widget.Holo.Light.EditText" with no positive result. 我已经尝试将EditText的样式设置为"@android:style/Widget.Holo.Light.EditText" ,没有任何正面结果。


#1楼

参考:https://stackoom.com/question/UN3C/设置EditText光标颜色


#2楼

It appears as if all the answers go around the bushes. 似乎所有的答案都围绕着灌木丛。

In your EditText , use the property: EditText ,使用属性:

android:textCursorDrawable="@drawable/black_cursor"

and add the drawable black_cursor.xml to your resources, as follows: 并将drawable black_cursor.xml添加到您的资源,如下所示:

<?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="#000000"/>
</shape>

This is also the way to create more diverse cursors, if you need. 如果需要,这也是创建更多样化游标的方法。


#3楼

In Layout 在布局中

<EditText  android:layout_width="match_parent" android:layout_height="wrap_content" android:textCursorDrawable="@drawable/color_cursor"/>

Then create drawable xml: color_cursor 然后创建drawable xml:color_cursor

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" ><size android:width="3dp" /><solid android:color="#FFFFFF"  />
</shape>

You have a white color cursor on EditText property. EditText属性上有一个白色光标。


#4楼

Its even easier than that. 它比这更容易。

<style name="MyTextStyle"><item name="android:textCursorDrawable">#000000</item>
</style>

This works in ICS and beyond. 这适用于ICS及更高版本。 I haven't tested it in other versions. 我没有在其他版本中测试它。


#5楼

There is a new way to change cursor color in latest Appcompact v21 在最新的Appcompact v21中有一种更改光标颜色的新方法
Just change colorAccent in style like this: 只需更改colorAccent样式,如下所示:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette--><!-- colorPrimary is used for the default action bar background --><item name="colorPrimary">#088FC9</item><!-- colorPrimaryDark is used for the status bar --><item name="colorPrimaryDark">#088FC9</item><!-- colorAccent is used as the default value for colorControlActivatedwhich is used to tint widgets --><!-- THIS IS WHAT YOU'RE LOOKING FOR --><item name="colorAccent">#0091BC</item>
</style>

Then apply this style on your app theme or activities. 然后在您的应用主题或活动上应用此样式。

Update : this way only works on API 21+ 更新 :这种方式仅适用于API 21+
Update 2 : I'm not sure the minimum android version that it can work. 更新2 :我不确定它可以工作的最低Android版本。
Tested by android version: 经Android版测试:

2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked

#6楼

For anyone that needs to set the EditText cursor color dynamically, below you will find two ways to achieve this. 对于需要动态设置EditText光标颜色的任何人,下面你会发现两种方法来实现这一点。


First, create your cursor drawable: 首先,创建你的光标drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle" ><solid android:color="#ff000000" /><size android:width="1dp" /></shape>

Set the cursor drawable resource id to the drawable you created (https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)): 将光标drawable资源id设置为您创建的drawable(https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)) :

try {Field f = TextView.class.getDeclaredField("mCursorDrawableRes");f.setAccessible(true);f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}

To just change the color of the default cursor drawable, you can use the following method: 要仅更改默认光标drawable的颜色,可以使用以下方法:

public static void setCursorDrawableColor(EditText editText, int color) {try {Field fCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");fCursorDrawableRes.setAccessible(true);int mCursorDrawableRes = fCursorDrawableRes.getInt(editText);Field fEditor = TextView.class.getDeclaredField("mEditor");fEditor.setAccessible(true);Object editor = fEditor.get(editText);Class<?> clazz = editor.getClass();Field fCursorDrawable = clazz.getDeclaredField("mCursorDrawable");fCursorDrawable.setAccessible(true);Drawable[] drawables = new Drawable[2];Resources res = editText.getContext().getResources();drawables[0] = res.getDrawable(mCursorDrawableRes);drawables[1] = res.getDrawable(mCursorDrawableRes);drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);fCursorDrawable.set(editor, drawables);} catch (final Throwable ignored) {}
}

设置EditText光标颜色相关推荐

  1. Android 设置EditText光标位置(转)

    Android 设置EditText光标位置 最后 CharSequence text = edtTxt_my_account_edit_nickname.getText();if (text ins ...

  2. android edittext背景颜色,Android 设置 EditText 背景颜色、背景图片

    Android 设置 EditText 背景颜色mView.findViewById(R.id.editText).setBackgroundResource(R.color.caution); An ...

  3. Android 设置EditText光标Cursor颜色及粗细

    2019独角兽企业重金招聘Python工程师标准>>> 在android的输入框里,如果要修改光标的颜色及粗细步骤如下两步即可搞定: 1.在资源文件drawable下新建一个光标控制 ...

  4. 利用运行时设置UITextField 光标颜色

    #import "XMGLoginRegisterTextField.h" #import <objc/runtime.h> @implementation XMGLo ...

  5. android设置webview光标颜色,CSS自定义设置元素闪烁光标颜色

    这次给大家带来CSS自定义设置元素闪烁光标颜色,CSS自定义设置元素闪烁光标颜色的注意事项有哪些,下面就是实战案例,一起来看一下. 前言因为业务需求, 要求我们的input框内的文本与悬浮的光标颜色不 ...

  6. android 设置EditText光标位置

    Android中有很多可编辑的弹出框,其中有些是让我们来修改其中的字符,这时光标位置定位在哪里呢? 刚刚解了一个bug是关于这个光标的位置的,似乎Android原生中这种情况是把光标定位到字符串的最前 ...

  7. Java设置edittext光标,如何改变的EditText光标高度?

    I want to change the EditText cursor height, does anyone know how? 解决方案 I had to dig far into the An ...

  8. 电脑设置鼠标光标颜色或大小时,点击之后窗口闪退

    问题如标题. 系统:win10 电脑品牌:联想 其他:安装有联想电脑管家 闪退的窗口如下 点击想要的颜色后便会闪退. 解决办法: 如上是官方客服提供的解决办法,试了一下,果不其然.

  9. 解决android手机EditText设置光标颜色,android:textCursorDrawable=@drawable/corner_cursor 华为手机无效果的问题

    app开发,根据产品需求,需要修改输入框内,光标的颜色, 需要增加一个属性, android:textCursorDrawable="@drawable/corner_cursor" ...

最新文章

  1. 发布文件打包springboit_程序安装包咋制作的?Qt程序打包三部曲,从应用程序到安装包...
  2. /usr/bin/perl^M: bad interpreter: No such file or directory
  3. Angular NgReflectProperty的设置位置 - 只有在调试模式下才设置该属性
  4. javascript动态创建radio button元素支持IE/Firefox
  5. HashSet存储自定义对象保证元素唯一性图解原理及代码优化
  6. bzoj 1858: [Scoi2010]序列操作
  7. md5加密、Des加密对称可逆加密、RSA非对称可逆加密、https单边验证、银行U盾双边认证...
  8. Java Web下访问外部jar,实例后的Object类型转化的问题
  9. [推荐]一个 1kb 的image placeholder的js库
  10. 实现类似shared_ptr的引用计数
  11. 微信计步器怎么不计步_微信运动不计步,如何解决
  12. MongoDB复制集同步慢问题分析
  13. wake on LAN: 三分钟实现从Linux和Windows设备上远程唤醒设备
  14. Window10主硬盘更换及系统重装
  15. C++(30)—奇偶数判断
  16. Latex编写时会遇到在作者名字上字母上加一声,二声,三声、四声或两点等标志
  17. QuartusII绑定引脚时出现错误:Error: Can‘t place multiple pins assigned to pin location Pin_108 (IOC_X34_Y2_N0)
  18. 数值逼近课程设计(1)——runge现象
  19. Linux怎么看磁盘设备名,Linux通过设备名称如何定位故障硬盘
  20. Pytorch打怪路(三)Pytorch创建自己的数据集2

热门文章

  1. Android 探究 LayoutInflater setFactory
  2. 奋斗---论门当户对
  3. 算法------判定字符是否唯一
  4. 算法--------打家劫舍(动态规划,Java版本)
  5. 算法-------寻找旋转排序数组中的最小值
  6. 解决编译不通过Could not find support-compat.aar (com.android.support:support-compat:26.0.0).
  7. Linux下find命令的使用(命令+案例)
  8. 数据量大mysql当月_mysql 1.5倍数据量导致20多倍的执行时间?
  9. 微信小游戏开发教程-游戏实现3
  10. Swift 扩展存储属性