如何编程更改EDITTEXT光标颜色在android系统? [英] How to Change programatically Edittext Cursor Color in android?
本文介绍了如何编程更改EDITTEXT光标颜色在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的Android提供的android:textCursorDrawable =@绘制/ black_color_cursor
XML中,但我的cunsurn是如何动态地改变改变光标颜色的android
在我的情况我已经设置光标绘制白色的,但我需要改变黑怎么办?
//设置一个EditText视图来获取用户输入
最终的EditText输入=新的EditText(nyactivity);
input.setTextColor(getResources()的getColor(R.color.black));
解决方案
使用一些反思的伎俩,我
Java的:
{尝试
// https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564
域F = TextView.class.getDeclaredField(mCursorDrawableRes);
f.setAccessible(真);
f.set(yourEditText,R.drawable.cursor);
}赶上(例外忽略){
}
XML:
<?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:形状=矩形> [固体机器人:颜色=#FF000000/> <大小的android:宽=1DP/>< /形状>
下面是您可以使用,这并不需要一个XML的方法:
公共静态无效setCursorDrawableColor(EditText上EDITTEXT,诠释颜色){
尝试{
现场fCursorDrawableRes = TextView.class.getDeclaredField(mCursorDrawableRes);
fCursorDrawableRes.setAccessible(真);
INT mCursorDrawableRes = fCursorDrawableRes.getInt(EDITTEXT);
现场fEditor = TextView.class.getDeclaredField(mEditor);
fEditor.setAccessible(真);
对象编辑器= fEditor.get(EDITTEXT);
类<> clazz所= editor.getClass();
现场fCursorDrawable = clazz.getDeclaredField(mCursorDrawable);
fCursorDrawable.setAccessible(真);
可绘制[] =可绘制新绘制对象[2];
。可绘[0] = editText.getContext()getResources()getDrawable(mCursorDrawableRes);
。可绘[1] = editText.getContext()getResources()getDrawable(mCursorDrawableRes);
可绘制[0] .setColorFilter(颜色,PorterDuff.Mode.SRC_IN);
可绘[1] .setColorFilter(颜色,PorterDuff.Mode.SRC_IN);
fCursorDrawable.set(编辑,图形);
}赶上(Throwable的忽略){
}
}
Here android provide android:textCursorDrawable="@drawable/black_color_cursor"
within xml, but my cunsurn is how to change dynamically change cursor color in android
in my case i have set cursor drawable white, but i need to change black How to do ?
// Set an EditText view to get user input
final EditText input = new EditText(nyactivity);
input.setTextColor(getResources().getColor(R.color.black));
解决方案
Using some reflection did the trick for me
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>
Here is a method that you can use that doesn't need an XML:
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];
drawables[0] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
drawables[1] = editText.getContext().getResources().getDrawable(mCursorDrawableRes);
drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
fCursorDrawable.set(editor, drawables);
} catch (Throwable ignored) {
}
}
这篇关于如何编程更改EDITTEXT光标颜色在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文