2013-12-17 5 views
10

Unten ist ein Screenshot eines benutzerdefinierten Textfelds, das ich in meiner Android App habe. Während Sie ein bestimmtes Wort eingeben, wird der Text für das Wort, das Sie gerade eingeben, grau hervorgehoben und der Text wird schwarz dargestellt, bis Sie die Leertaste drücken. Der Text wird dann wie erwartet weiß. Gibt es eine Möglichkeit, die Farbe der Hervorhebung und des markierten Textes zu ändern?Wie man die Hervorhebungsfarbe am EditText Feld ändert

enter image description here

mein Text bearbeiten xml sieht wie folgt aus

<EditText 
        android:id="@+id/searchField" 
        android:layout_width="160dp" 
        android:layout_height="44dp" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="60dp" 
        android:background="@null" 
        android:cursorVisible="true" 
        android:ems="10" 
        android:textColor="@color/white" 
        android:textColorHighlight ="#ff0000" 
        android:textCursorDrawable="@null" > 
       </EditText> 

das gesamte Layout

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <!-- 
     As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. 
    --> 

    <RelativeLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#fff" > 



     <fragment 
      android:id="@+id/map" 
      android:name="com.sapientnitro.inhouse.drop.components.DRPCustomMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 



     <ImageButton 
      android:id="@+id/btn_center_local" 
      android:layout_width="44dp" 
      android:layout_height="44dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentRight="true" 
      android:layout_marginBottom="50dp" 
      android:layout_marginRight="15dp" 
      android:background="@drawable/btn_center_on_local_up" /> 

     <RelativeLayout 
      android:id="@+id/top_bar" 
      android:layout_width="match_parent" 
      android:layout_height="70dp" 
      android:background="#ddffffff" > 

      <ImageButton 
       android:id="@+id/btn_menu" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_alignParentBottom="true" 
       android:layout_marginBottom="10dp" 
       android:layout_marginRight="12dp" 
       android:layout_toLeftOf="@+id/btn_search" 
       android:background="@drawable/btn_menu_up" /> 


      <ImageButton 
       android:id="@+id/btn_create" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_alignParentBottom="true" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="12dp" 
       android:layout_toRightOf="@+id/btn_search" 
       android:background="@drawable/btn_create_up" /> 

      <ImageButton 
       android:id="@+id/btn_search" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:layout_marginBottom="10dp" 
       android:background="@drawable/btn_search_up" /> 
     </RelativeLayout> 

     <LinearLayout 
      android:id="@+id/search" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#ddffffff" 
      android:orientation="vertical" > 

      <RelativeLayout 
       android:id="@+id/search_bar" 
       android:layout_width="match_parent" 
       android:layout_height="70dp" 
       android:background="#dd00cccb" > 

       <ImageView 
        android:id="@+id/searchBox" 
        android:layout_width="238dp" 
        android:layout_height="44dp" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="20dp" 
        android:src="@drawable/search_field" /> 

       <ImageView 
        android:id="@+id/clear" 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="220dp" 
        android:src="@drawable/btn_clear_field" /> 

       <EditText 
        android:id="@+id/searchField" 
        android:layout_width="160dp" 
        android:layout_height="44dp" 
        android:layout_alignParentLeft="true" 
        android:layout_centerVertical="true" 
        android:layout_marginLeft="60dp" 
        android:background="@null" 
        android:cursorVisible="true" 
        android:ems="10" 
        android:textColor="@color/white" 
        android:textColorHighlight ="@color/white" 
        android:textCursorDrawable="@null" > 
       </EditText> 

       <TextView 
        android:id="@+id/cancelBTN" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="16dp" 
        android:textColor="@color/white" 
        android:textSize="22dp" 
        android:text="@string/cancel" /> 

      </RelativeLayout> 

      <ScrollView 
       android:id="@+id/search_results_container" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:paddingTop="2dp" 
       android:scrollbars="none" > 

       <LinearLayout 
        android:id="@+id/search_results_content" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical" > 

        <RelativeLayout 
         android:id="@+id/header_artists" 
         android:layout_width="match_parent" 
         android:layout_height="50dp" 
         android:background="#dd00cccb" /> 

        <HorizontalScrollView 
         android:id="@+id/results_artists" 
         android:layout_width="match_parent" 
         android:layout_height="150dp" /> 

        <RelativeLayout 
         android:id="@+id/header_followers" 
         android:layout_width="match_parent" 
         android:layout_height="50dp" 
         android:background="#dd00cccb" /> 

        <HorizontalScrollView 
         android:id="@+id/results_followers" 
         android:layout_width="match_parent" 
         android:layout_height="150dp" /> 

        <RelativeLayout 
         android:id="@+id/header_places" 
         android:layout_width="match_parent" 
         android:layout_height="50dp" 
         android:background="#dd00cccb" /> 

        <HorizontalScrollView 
         android:id="@+id/results_places" 
         android:layout_width="match_parent" 
         android:layout_height="150dp" /> 
       </LinearLayout> 
      </ScrollView> 
     </LinearLayout> 
    </RelativeLayout> 


    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="255dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#dd00cccb" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:listSelector="@drawable/selector_transparent" /> 

    <ListView 
     android:id="@+id/right_drawer" 
     android:layout_width="255dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="end" 
     android:background="#dd00cccb" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:listSelector="@drawable/selector_transparent" /> 

</android.support.v4.widget.DrawerLayout> 
+0

Wie haben Sie Ihre Kopf-/EditText wie das Stil? – Tyler

+0

wie was? setze einfach den Hintergrund auf null und setze ein Bild darunter – erik

+0

Hast du eine Lösung gefunden? Ich habe das gleiche Problem :( –

Antwort

0

Sie einen Selektor verwenden sollten Text colors` zu ändern:

<?xml version="1.0" encoding="UTF-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<!-- Pressed State --> 
<item android:state_pressed="true" 
    android:color="#FF0000" /> 
<!-- Focused State --> 
<item android:state_focused="true" 
    android:color="#FF0000" /> 
<!-- Default State --> 
<item android:color="#FFFFFF" /> 

Und dann stellen Sie Ihre Textfarbe Eigenschaft @ ziehbar/selector_name

+0

Nein, das funktioniert auch nicht .. mit Ihrem genauen Code oben, wäre mein Text rot, nachdem ein Wort abgeschlossen ist, aber immer noch mit grau über schwarz markiert, genau wie mein Bildschirm oben geschossen. – erik

1

Sie android:textColorHighlight im EditText Element in ihr Layout XML-Datei

5

Einfach, benutzen Sie einfach TextHightLight Property im XML-Modus verwenden sollten

android:textColorHighlight="#ff0000" 

[Bild hier angebracht ist, da unten für einen Kommentar]

und wenn ich das tue, das ist, wie es

sieht

enter image description here

+0

Es tut mir leid, ich hätte dies in meiner ursprünglichen Frage gepostet, aber ich habe es bereits versucht .. es tut nichts! – erik

+0

sollte es funktionieren. eigentlich habe ich die selbe Art der Anpassung, die ich an einem Projekt verwendet habe. und ich habe gerade Ihren genauen Code in eine Schein-App ausprobiert und es funktioniert einfach gut. – Kirk

+0

Ich schätze, Sie passen Ihre Aktionsleiste an. kannst du mir deine Aktionsleiste oder den angepassten XML-Code posten, damit ich es herausfinden kann? – Kirk

0

Verwendung dieser Code, könnte seine Hilfe Sie

android:textColorHighlight="your color code" 
14
sein

konnte ich das Highlight meiner Bearbeiten von Text ändern durch das Hinzufügen dieser Zeile meiner themes.xml Datei:

<item name="android:textColorHighlight">@color/m_highlight_blue</item> 
+0

Ist es möglich, die Farbe der Selection-Handler zu ändern, die Sie ziehen können? –

+0

Wissen Sie, wie Sie das programmatisch einstellen? – Kuls

1

In Materialdesign erhalten Sie die defaul t Aufstieg Farbe im Stil, so dass die gesamte Textauswahl und andere Thema würde entsprechend dieser Farbe sein. Sie können diesen Aufstieg Farbe in folgender Weise ändern -

Zum style.xml und ändern Sie die folgende Farbe -

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
     <item name="colorAccent">@color/colorAccent</item> // Replace this color with your own choice 
    </style> 
2

Und wenn Sie den Marker Griff ändern müssen, dann sollten Sie ändern

<item name="colorAccent">@color/edittext_handle_selection</item> 
<item name="android:textColorHighlight">@color/edittext_selection</item> 
2

In Code:

et.setHighlightColor(ContextCompat.getColor(getContext(), R.color.highlight)); 
Verwandte Themen