Android 更改编辑文本光标颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11649234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
change edittext cursor color
提问by Adem
How can I change EditText's
cursor's colorprogrammatically ?
如何以编程方式更改EditText's
光标的颜色?
In android 4.0 and above, cursor coloris white. and if EditText
s background is also white, it becomes invisible.
在 android 4.0 及更高版本中,光标颜色为白色。如果EditText
s 的背景也是白色的,它就会变得不可见。
回答by user370305
In your EditText properties, there is an attribute android:textCursorDrawable
在您的 EditText 属性中,有一个属性 android:textCursorDrawable
Now set it to @nulllike,
现在将其设置为@null 之类的,
android:textCursorDrawable="@null"
android:textCursorDrawable="@null"
So now your EditText Cursor is same as your EditText TextColor.
所以现在您的 EditText Cursor 与您的 EditText TextColor 相同。
Reference From Set EditText cursor color
回答by Adem
I found a way to fix this. It is not the greatest solution, but it works.
我找到了解决这个问题的方法。这不是最好的解决方案,但它有效。
Unfortunately, I can only use static color for the cursor color.
不幸的是,我只能使用静态颜色作为光标颜色。
First, I define a black cursor in 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>
Next I define a sample EditText in layouts.
接下来我在布局中定义一个示例 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>
When I want to create an EditText at runtime, I use this:
当我想在运行时创建一个 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);
Now you have an EditText view that has a black cursor. Maybe somebody can improve on my solution so that the cursor can be changed at runtime.
现在您有一个带有黑色光标的 EditText 视图。也许有人可以改进我的解决方案,以便可以在运行时更改光标。
回答by Jared Rummler
Here is, what I think, a better solution than what @Adem posted.
这是我认为比@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:
<?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>
回答by kc ochibili
improving on Jared Rummler's answer
改进 Jared 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) {
}
create custom_cursor.xml in res/drawable folder:
在 res/drawable 文件夹中创建 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" />
</shape>
XML:
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>
回答by shkschneider
What about the styles.xml
using AppCompat-v7?
怎么样styles.xml
使用程序兼容性-V7?
<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>
Works for me (this example is simplistic).
对我有用(这个例子很简单)。
回答by Aj 27
You may Set the android:textCursorDrawable
attribute to @null
that will result in the use of android:textColor
as the cursor color.
您可以将android:textCursorDrawable
属性设置为@null
将导致android:textColor
用作光标颜色。
Attribute textCursorDrawable
is available in API level 12 and higher...
属性textCursorDrawable
在 API 级别 12 及更高级别中可用...
Thanks...
谢谢...
回答by Sergey Galin
So, here's the final version - doesn't need the XML and works as @null but programmatically:
所以,这是最终版本 - 不需要 XML 并且以@null 的方式工作,但以编程方式:
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set((TextView)yourEditText, 0);
} catch (Exception ignored) {
}
The only problem is that it may stop working one day with some new version of Android.
唯一的问题是它可能有一天会在某些新版本的 Android 上停止工作。
PS I tested it on 4.1.2 to 5.1.
PS 我在 4.1.2 到 5.1 上测试过。
回答by Rakesh Yadav
Set Via AppTheme
通过 AppTheme 设置
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:spinnerStyle">@style/spinner_style</item>
<!--Add This-->
<item name="editTextStyle">@style/EdittextStyle</item>
<item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
</style>
<!--New EditText Style-->
<style name="EdittextStyle" parent="Widget.AppCompat.EditText">
<item name="android:background">?attr/editTextBackground</item>
<item name="android:textColor">?attr/editTextColor</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<!--<item name="android:textCursorDrawable">@drawable/abc_text_cursor_material</item>-->
<item name="colorControlNormal">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorAccent</item>
<item name="colorControlHighlight">@color/colorAccent</item>
</style>