2014-09-23 15 views
16

In Android possiamo cambiare il colore del cursore tramite:Come cambiare a livello di codice del cursore Citte di Edittext in Android?

android:textCursorDrawable="@drawable/black_color_cursor".

Come possiamo farlo dinamicamente?

Nel mio caso ho impostato il cursore su bianco, ma ho bisogno di cambiare il nero Come fare?

// Set an EditText view to get user input 
    final EditText input = new EditText(nyactivity); 
    input.setTextColor(getResources().getColor(R.color.black)); 
+0

follow [questo link] (https://stackoverflow.com/questions/7238450/set-edittext-cursor-color) , il meglio per cui andare. – Krantiz

risposta

56

Utilizzando qualche riflessione ha fatto il trucco per me

Java:

// 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); 

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> 

Ecco un metodo che è possibile utilizzare che non ha bisogno di un XML:

public static void setCursorColor(EditText view, @ColorInt int color) { 
    try { 
    // Get the cursor resource id 
    Field field = TextView.class.getDeclaredField("mCursorDrawableRes"); 
    field.setAccessible(true); 
    int drawableResId = field.getInt(view); 

    // Get the editor 
    field = TextView.class.getDeclaredField("mEditor"); 
    field.setAccessible(true); 
    Object editor = field.get(view); 

    // Get the drawable and set a color filter 
    Drawable drawable = ContextCompat.getDrawable(view.getContext(), drawableResId); 
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); 
    Drawable[] drawables = {drawable, drawable}; 

    // Set the drawables 
    field = editor.getClass().getDeclaredField("mCursorDrawable"); 
    field.setAccessible(true); 
    field.set(editor, drawables); 
    } catch (Exception ignored) { 
    } 
} 
+2

Il tuo codice funziona bene! – user4152

+2

Grazie, sei un vero toccasana. – Baggers

4
android:textCursorDrawable="@null" 

Poi nell'applicazione:

final EditText input = new EditText(nyactivity); 
input.setTextColor(getResources().getColor(R.color.black)); 

Get from here

+0

scusate ma non ho alcun xml per questo come fare. ?? –

5

Questa è una versione riscritta della funzione da @Jared Rummler con un paio di miglioramenti:

  • Supporto per Android 4.0.x
  • La funzione speciale getDrawable(Context, int) si chiama getDrawable(int) obsoleta per API 22 e versioni successive.
private static final Field 
     sEditorField, 
     sCursorDrawableField, 
     sCursorDrawableResourceField; 

static { 
    Field editorField = null; 
    Field cursorDrawableField = null; 
    Field cursorDrawableResourceField = null; 
    boolean exceptionThrown = false; 
    try { 
     cursorDrawableResourceField = TextView.class.getDeclaredField("mCursorDrawableRes"); 
     cursorDrawableResourceField.setAccessible(true); 
     final Class<?> drawableFieldClass; 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { 
      drawableFieldClass = TextView.class; 
     } else { 
      editorField = TextView.class.getDeclaredField("mEditor"); 
      editorField.setAccessible(true); 
      drawableFieldClass = editorField.getType(); 
     } 
     cursorDrawableField = drawableFieldClass.getDeclaredField("mCursorDrawable"); 
     cursorDrawableField.setAccessible(true); 
    } catch (Exception e) { 
     exceptionThrown = true; 
    } 
    if (exceptionThrown) { 
     sEditorField = null; 
     sCursorDrawableField = null; 
     sCursorDrawableResourceField = null; 
    } else { 
     sEditorField = editorField; 
     sCursorDrawableField = cursorDrawableField; 
     sCursorDrawableResourceField = cursorDrawableResourceField; 
    } 
} 

public static void setCursorColor(EditText editText, int color) { 
    if (sCursorDrawableField == null) { 
     return; 
    } 
    try { 
     final Drawable drawable = getDrawable(editText.getContext(), 
       sCursorDrawableResourceField.getInt(editText)); 
     drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN); 
     sCursorDrawableField.set(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN 
       ? editText : sEditorField.get(editText), new Drawable[] {drawable, drawable}); 
    } catch (Exception ignored) { 
    } 
} 

private static Drawable getDrawable(Context context, int id) { 
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { 
     return context.getResources().getDrawable(id); 
    } else { 
     return context.getDrawable(id); 
    } 
} 
+0

ottimo, ma penso che sarebbe meglio sostituire il metodo personalizzato 'getDrawable (context, resId)' con 'ContextCompat.getDrawable (context, resId)' - lo stesso metodo appena estratto dalla scatola;) –

1

siamo riusciti a farlo:

  1. Creazione di un file di layout con un semplice EditText e il colore del cursore impostato in XML su di esso.
  2. gonfiando
  3. Utilizzando l'EditText come si usa un livello di codice creato uno
Problemi correlati