更改编辑文本光标颜色

问题描述:

如何更改EditText's光标的颜色以编程方式?更改编辑文本光标颜色

在android 4.0及以上版本中,光标颜色为白色。并且如果EditText的背景也是白色的,则它变得不可见。

在你的EditText属性外,还有一个属性android:textCursorDrawable

现在它设置为@null一样,

android:textCursorDrawable="@null"

所以,现在你的EditText光标是与您的EditText TEXTCOLOR。

参考从Set EditText cursor color

+5

我需要它编程,而不是基于xml的 – Adem 2012-07-25 12:24:43

+0

感谢您提供有用的信息 – raju 2012-11-20 13:18:38

+1

@Adem如果您在XML中设置此项,您可以将任何颜色以编程方式设置为文本颜色。 – user363349 2013-04-23 15:02:57

我找到了一种方法来解决这个问题。这不是最好的解决方案,但它有效。

不幸的是,我只能对游标颜色使用静态颜色。

首先,我定义在抽拉

<?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> 

黑色光标接着我定义布局的样品的EditText。

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textCursorDrawable="@drawable/blackpipe" 
     > 
    </EditText> 

当我想在运行时创建一个EditText,我用这个:

AttributeSet editText30AttributeSet = null; 
int res = getResources().getIdentifier("edit30", "layout", getPackageName());//edit30 is EditText layout 
XmlPullParser parser = getResources().getXml(res); 
int state=0; 
do { 
    try { 
     state = parser.next(); 
    } catch (Exception e1) { 
     e1.printStackTrace(); 
    }  
    if (state == XmlPullParser.START_TAG) { 
     if (parser.getName().equals("EditText")) { 
      editText30AttributeSet = Xml.asAttributeSet(parser); 
      break; 
     } 
    } 
} while(state != XmlPullParser.END_DOCUMENT); 
EditText view = new EditText(getContext(),editText30AttributeSet); 

现在你有,有一个黑色的光标一个EditText视图。也许有人可以改进我的解决方案,以便游标可以在运行时更改。

+0

很棒的工作,谢谢!像定制EditText组件的魅力一样工作。 – Divers 2014-03-26 11:44:01

这是,我认为,比@Adem发布的更好的解决方案。

的Java:

try { 
    // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564 
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); 
    f.setAccessible(true); 
    f.set(yourEditText, R.drawable.cursor); 
} catch (Exception ignored) { 
} 

XML:

<?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> 

,您可以设置android:textCursorDrawable属性@null,这将导致在使用android:textColor作为光标颜色。

属性textCursorDrawable是在API级别12和更高的可...

谢谢...

怎么样styles.xml使用程序兼容性-V7?

<item name="colorControlNormal">@color/accentColor</item> 
<item name="colorControlActivated">@color/accentColor</item> 
<item name="colorControlHighlight">@color/accentColor</item> 

适用于我(这个例子很简单)。

+0

这些重音符号是否适用于前棒棒糖设备上的EditText游标? – 2015-05-20 22:15:26

+0

不,它然后采用默认系统的光标(可悲)。已经发布了很好的答案,方式更完整。 – shkschneider 2015-05-21 08:05:40

上贾里德Rummler的回答改善

的Java:

try { 
    // https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564 
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); 
    f.setAccessible(true); 
    f.set(yourEditText, R.drawable.cursor); 
} catch (Exception ignored) { 
} 

创建资源custom_cursor.xml /绘制的文件夹:

<?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" /> 

XML:

<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/custom_cursor" 
    > 
</EditText> 

所以,这是最后的版本锡永 - 不需要XML和@null但编程工作:

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

唯一的问题是,它可能会停止工作一天,也与Android的一些新的版本。

PS我测试4.1.2到5.1。