Ustaw programowo textCursorDrawable

Jeśli dodam EditText w XML mogę ustawić textCursorDrawable="@null":

<EditText
    android:id="@+id/txtE3Casecode4"
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="#C7C7C5"
    android:textCursorDrawable="@null"
    android:ems="10"
    android:inputType="number"
    android:maxLength="2"
    android:text="01"
    android:textColor="#000000" />

Teraz rysuję EditText w Javie. Chcę set android:textCursorDrawable="@null".

LinearLayout.LayoutParams paramtext = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.WRAP_CONTENT);
EditText txtOther = new EditText(this);
txtOther.setLayoutParams(paramtext);
txtOther.setBackgroundColor(Color.WHITE);
txtOther.setTextColor(Color.BLACK);
txtOther.setId(99999);
// txtOther.setCursorDrawable ?                                

Jak to ustawić?

Author: Cœur, 2012-07-19

3 answers

Nie ma publicznego API do ustawiania rysowalnego kursora. Możesz ustawić go programowo za pomocą reflection. Pole mCursorDrawableRes nie uległo zmianie, więc powinno działać na wszystkich urządzeniach, chyba że producent coś zmienił lub zostanie później zmienione.

Użyj refleksji, aby ustawić kursor:

EditText yourEditText = new EditText(context);

...

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) {
}

Zdefiniuj kursor rysowalny w aplikacji:

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

Inne podejście:

Możesz również ustawić kolor kursora za pomocą następującej metody:

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) {
    } 
} 
 51
Author: Jared Rummler,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-04 22:29:14

Odpowiedź brzmi ---------------------> to nieosiągalne.

W sumie też spotkałem się z tym problemem w mojej pracy, ale po sprawdzeniu doc i kodu źródłowego google ' a uznałem, że nie można ustawić tego atrybutu w kodzie Javy.

W klasie TextView znajdziesz poniższy kod

case com.android.internal.R.styleable.TextView_textCursorDrawable:
    mCursorDrawableRes = a.getResourceId(attr, 0);
    break;

Ale metoda textCursorDrawable() istnieje tylko w R.attr, Jeśli chcesz ustawić ten atrybut, możesz wywołać metodę construct poniżej, włączając editText w pliku XML.

public EditText(Context context, AttributeSet attrs) {
    this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
 1
Author: cinsondev,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-24 21:57:46

Jeśli używasz AppCompat library i Twoja aktywność rozszerza AppCompatActivity, możesz ustawić kolor kursora tekstowego (dla wszystkich EditText) za pomocą stylu XML colorAccent :

<style name="AppTheme" parent="@style/Theme.AppCompat">
    <item name="colorAccent">#FF4081</item>
</style>

Działa na Androidzie 5+ i jest wstecznie kompatybilny ze starszymi wersjami Androida od Android 4.4 (API 19) do Android 4.0 (API 14). Uwaga: jeśli używasz AutoCompleteTextView, upewnij się, że każda jego niestandardowa podklasa rozszerza AppCompatAutoCompleteTextView, w przeciwnym razie tekst stylizacja kursora nie będzie działać.

Zobacz także ten poradnik, Jak zastosować akcent kolorowy (tint) do przycisków dialogowych: motyw Android v21.Akcent kolorów Appcompat jest ignorowany, brak wypełnienia w oknach dialogowych

 0
Author: Mr-IDE,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-13 19:37:58