Ustaw kolor kursora EditText

Mam problem, w którym używam motywu Holo Androida na projekcie tabletu. Mam jednak fragment na ekranie, który ma białe tło. Dodaję do tego fragmentu komponent EditText. Próbowałem nadpisać motyw ustawiając Tło Holo.Lekkie zasoby tematyczne. Jednak mój kursor tekstowy (carat) pozostaje biały, a co za tym idzie, niewidoczny na ekranie (słabo go widzę w polu edittext..).

Czy ktoś wie jak mogę zmusić EditText do używania ciemniejszego kolor kursora? Próbowałem ustawić styl EditText na "@android:style/Widget.Holo.Light.EditText" bez pozytywnego wyniku.

Author: Jonik, 2011-08-30

15 answers

Ustawienie atrybutu android:textCursorDrawable na @null powinno spowodować użycie android:textColor jako koloru kursora.

Atrybut "textCursorDrawable" jest dostępny na poziomie API 12 i wyższym
 964
Author: Dean,
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-01-28 09:11:14

W Układzie

<EditText  
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/color_cursor"
    />

Następnie utwórz drawable xml: color_cursor

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="3dp" />
    <solid android:color="#FFFFFF"  />
</shape>

Na właściwości EditText znajduje się kursor koloru białego.

 359
Author: star18bit,
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
2018-02-24 20:00:48

Wygląda na to, że wszystkie odpowiedzi chodzą po krzakach.

W twoim EditText Użyj właściwości:

android:textCursorDrawable="@drawable/black_cursor"

I dodaj drawable black_cursor.xml do swoich zasobów, w następujący sposób:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <size android:width="1dp" />
    <solid android:color="#000000"/>
</shape>

Jest to również sposób na tworzenie bardziej zróżnicowanych kursorów, jeśli potrzebujesz.

 50
Author: Zach-M,
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
2014-10-31 10:38:36

Istnieje nowy sposób zmiany koloru kursora w najnowszej Appcompact v21
Po prostu zmień colorAccent w takim stylu:

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->

    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#088FC9</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#088FC9</item>

    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <!-- THIS IS WHAT YOU'RE LOOKING FOR -->
    <item name="colorAccent">#0091BC</item> 
</style>

Następnie zastosuj ten styl do motywu aplikacji lub działań.

Update: ten sposób działa tylko na API 21+
Aktualizacja 2: nie jestem pewien, czy Minimalna wersja Androida może działać.
Testowane przez wersję Androida:

2.3.7 - didn't work
4.4.4 - worked
5.0 - worked
5.1 - worked
 40
Author: R4j,
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
2016-01-08 02:52:31

Znalazłem odpowiedź:)

Ustawiłem styl editText tematu na:

<item name="android:editTextStyle">@style/myEditText</item>

Następnie użyłem następującego drawable, aby ustawić kursor:

`

<style name="myEditText" parent="@android:style/Widget.Holo.Light.EditText">
    <item name="android:background">@android:drawable/editbox_background_normal</item>
    <item name="android:textCursorDrawable">@android:drawable/my_cursor_drawable</item>
    <item name="android:height">40sp</item>
</style>

`

Android: textCursorDrawable jest tutaj kluczem.

 36
Author: dineth,
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-10-11 11:27:28

Dla każdego, kto musi dynamicznie ustawić kolor kursora EditText, Poniżej znajdziesz dwa sposoby, aby to osiągnąć.


Najpierw Utwórz rysowalny kursor:

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

Ustaw identyfikator zasobu do rysowania kursora na utworzony (https://github.com/android/platform_frameworks_base/blob/kitkat-release/core/java/android/widget/TextView.java#L562-564">source)):

try {
    Field f = TextView.class.getDeclaredField("mCursorDrawableRes");
    f.setAccessible(true);
    f.set(yourEditText, R.drawable.cursor);
} catch (Exception ignored) {
}

Aby zmienić kolor domyślnego rysowalnego kursora, możesz użyć następujących "metoda": {]}

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];
        Resources res = editText.getContext().getResources();
        drawables[0] = res.getDrawable(mCursorDrawableRes);
        drawables[1] = res.getDrawable(mCursorDrawableRes);
        drawables[0].setColorFilter(color, PorterDuff.Mode.SRC_IN);
        drawables[1].setColorFilter(color, PorterDuff.Mode.SRC_IN);
        fCursorDrawable.set(editor, drawables);
    } catch (final Throwable ignored) {
    }
}
 24
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
2016-01-08 13:49:25

Spóźnienie na imprezę, Oto moja odpowiedź,

To jest dla ludzi, którzy Nie chcą zmienić colorAccent w swoim motywie nadrzędnym, ale chcą zmienić EditText atrybuty!

Ta odpowiedź demos jak zmienić ......

  1. Kolor linii dolnej
  2. Kolor kursora
  3. KOLOR WSKAŹNIKA kursora (użyłem własnego obrazu).......... z EditText za pomocą stylu zastosowanego do motywu aktywności.

Tutaj wpisz opis obrazka

<android.support.v7.widget.AppCompatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hey" />

Przykład:

<style name="AppTheme.EditText" parent="@style/Widget.AppCompat.EditText">
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">#8AFFFFFF</item>
    <item name="android:background">@drawable/edit_text_background</item> // background (bottom line at this case)
    <item name="android:textCursorDrawable">@color/white</item>  // Cursor
    <item name="android:textSelectHandle">@drawable/my_white_icon</item> // For pointer normal state and copy text state
    <item name="android:textSelectHandleLeft">@drawable/my_white_icon</item>
    <item name="android:textSelectHandleRight">@drawable/my_white_icon</item>
</style>

Teraz Utwórz drawable (edit_text_background) Dodaj zasób XML dla tła!Możesz dostosować, jak chcesz!

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:bottom="0dp"
        android:left="-3dp"   
        android:right="-3dp"
        android:top="-3dp">

        <shape android:shape="rectangle">
            <stroke
                android:width="1dp"
                android:color="@color/white"/>
        </shape>
    </item>
    </layer-list>

Teraz tak jak ty Ustawiłeś ten styl w swoim motywie aktywności.

Przykład:

W swojej aktywności masz motyw, Ustaw ten niestandardowy motyw editText na ten.

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Your Theme data -->
    <item name="editTextStyle">@style/AppTheme.EditText</item> // inculude this
</style>
 10
Author: Charuක,
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-02-02 14:13:31
Edittext cursor color you want changes your color.
   <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textCursorDrawable="@drawable/color_cursor"
    />

Następnie utwórz drawalble xml: color_cursor

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="3dp" />
    <solid android:color="#FFFFFF"  />
</shape>
 7
Author: influx,
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-01-06 18:09:29

Dla mnie zmodyfikowałem zarówno Kolory AppTheme, jak i wartości.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorControlNormal">@color/yellow</item>
    <item name="colorAccent">@color/yellow</item>
</style>
Oto kolory.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="yellow">#B7EC2A</color>
</resources>

Wyjąłem atrybut android: textCursorDrawable na @null, który umieściłem wewnątrz stylu editText. Kiedy próbowałem tego użyć, Kolory się nie zmieniły.

 6
Author: Emily Alexandra Conroyd,
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
2016-02-22 16:53:22

Wow jestem naprawde spozniony na ta impreze ale miala aktywnosc 17 dni temu Trzeba by się zastanowić nad zamieszczeniem jakiej wersji Androida używamy do odpowiedzi więc od teraz ta odpowiedź działa z Androidem 2.1 i nowszymi Przejdź do RES / VALUES / STYLES i dodaj linie kodu poniżej, a kursor będzie czarny

    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!--<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">-->
    <!-- Customize your theme here. -->
    <item name="colorControlActivated">@color/color_Black</item>
    <!--Sets COLOR for the Cursor in EditText  -->
</style>

Będziesz potrzebował tej linii kodu w folderze RES/COLOR

<color name="color_Black">#000000</color>

Dlaczego tak późno ? Miło byłoby rozważyć jakąś formę kategorii dla wielu Głowa Androida Potwór stał!

 4
Author: Grendel,
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-08-27 00:39:53

Editcolor.xml

android:textCursorDrawable="@drawable/editcolor"

W pliku xml Ustaw kod koloru edittext koloru tła

 3
Author: Avnish Titoriya,
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-04-28 06:45:51

To nawet łatwiejsze.

<style name="MyTextStyle">
    <item name="android:textCursorDrawable">#000000</item>
</style>
To działa w ICS i nie tylko. Nie testowałem go w innych wersjach.
 1
Author: Clark Will Battle,
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
2013-06-11 23:05:30

Użyj tego

Android: textCursorDrawable=" @ color / white "

 1
Author: Mohammed Alzoubi,
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-08-09 11:40:22

Jedyną poprawną odpowiedzią powinna być zmiana tematu działania: <item name="colorAccent">#000000</item> Nie powinieneś używać android:textCursorDrawable do @null, ponieważ dotyczy to tylko samego kursora, ale nie pinu pod kursorem, jeśli chcesz przeciągnąć kursor. Rozwiązanie tematyczne jest najpoważniejsze.

 1
Author: JPhi Denis,
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-10-16 12:05:31

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">#36f0ff</item>
    <item name="colorPrimaryDark">#007781</item>
    <item name="colorAccent">#000</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

Zmień kolor colouraccent w stylach.xm, that ' s it simple

 0
Author: Abhishek Saini,
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
2016-08-25 14:26:14