如何以编程方式更改EditText's 光标的颜色?
在android 4.0及更高版本中,光标颜色为白色。并且如果EditTexts的背景也是白色,它就变得不可见。
Answers:
在您的EditText属性中,有一个属性 android:textCursorDrawable
现在将其设置为@null,
android:textCursorDrawable="@null"
因此,现在您的EditText光标与您的EditText TextColor相同。
引用来自于Set EditText光标的颜色
我找到了解决此问题的方法。这不是最大的解决方案,但它可以工作。
不幸的是,我只能将静态颜色用作光标颜色。
首先,我在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>
接下来,我在布局中定义一个示例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视图。也许有人可以改进我的解决方案,以便可以在运行时更改光标。
我认为,这是一种比@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>
改善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.custom_cursor);
} catch (Exception ignored) {
}
在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:
<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>
怎么样styles.xml使用程序兼容性-V7?
<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>
为我工作(此示例很简单)。
通过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>