Wie kann ich EditText's die Farbe des Cursors programmgesteuert ändern ?
In Android 4.0 und höher ist die Cursorfarbe weiß. und wenn der EditTextHintergrund ebenfalls weiß ist, wird er unsichtbar.
Antworten:
In Ihren EditText-Eigenschaften gibt es ein Attribut android:textCursorDrawable
Jetzt setze es auf @null wie,
android:textCursorDrawable="@null"
Ihr EditText-Cursor entspricht nun Ihrem EditText-TextColor.
Referenz von Set EditText Cursorfarbe
Ich habe einen Weg gefunden, dies zu beheben. Es ist nicht die beste Lösung, aber es funktioniert.
Leider kann ich nur statische Farben für die Cursorfarbe verwenden.
Zuerst definiere ich einen schwarzen Cursor in 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>
Als nächstes definiere ich einen Beispiel-EditText in Layouts.
<?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>
Wenn ich zur Laufzeit einen EditText erstellen möchte, verwende ich Folgendes:
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);
Jetzt haben Sie eine EditText-Ansicht mit einem schwarzen Cursor. Vielleicht kann jemand meine Lösung verbessern, so dass der Cursor zur Laufzeit geändert werden kann.
Hier ist, was ich denke, eine bessere Lösung als das, was @Adem gepostet hat.
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>
Verbesserung der Antwort von 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) {
}
Erstellen Sie custom_cursor.xml im Ordner res / 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>
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>
Was ist mit der styles.xmlVerwendung von AppCompat-v7?
<item name="colorControlNormal">@color/accentColor</item>
<item name="colorControlActivated">@color/accentColor</item>
<item name="colorControlHighlight">@color/accentColor</item>
Funktioniert für mich (dieses Beispiel ist simpel).
Stellen Sie über AppTheme ein
<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>
Hier ist also die endgültige Version - benötigt kein XML und funktioniert als @null, aber programmgesteuert:
try {
Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set((TextView)yourEditText, 0);
} catch (Exception ignored) {
}
Das einzige Problem ist, dass es eines Tages möglicherweise nicht mehr mit einer neuen Version von Android funktioniert.
PS Ich habe es auf 4.1.2 bis 5.1 getestet.