web-dev-qa-db-ja.com

editTextフィールドのハイライト色を変更する方法

以下は、Android app。スペースバーを押すとテキストが黒になり、その時点でテキストが期待通りに白くなりますが、ハイライトとハイライトされるテキストの色を変更する方法はありますか?

enter image description here

私の編集テキストXMLは次のようになります

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

レイアウト全体

<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>
19
erik

この行をthemes.xmlファイルに追加することで、編集テキストのハイライトを変更できました。

<item name="Android:textColorHighlight">@color/m_highlight_blue</item>
20
l-l

単純、XMLモードでTextHightLightプロパティを使用

Android:textColorHighlight="#ff0000"

[ここに添付されている画像は、以下のコメントのためです]

そして、私がそうするとき、これはそれがどのように見えるかです

enter image description here

10
Kirk

マーカーハンドルを変更する必要がある場合は、変更する必要があります

<item name="colorAccent">@color/edittext_handle_selection</item>
<item name="Android:textColorHighlight">@color/edittext_selection</item>
4
Abhijeet

コード内:

et.setHighlightColor(ContextCompat.getColor(getContext(), R.color.highlight));
2
Cristan

レイアウトxmlファイルのEditText要素でAndroid:textColorHighlightを使用する必要があります

1
Dreagen

マテリアルデザインでは、スタイルのデフォルトのアセントカラーが取得されるため、すべてのテキスト選択およびその他のテーマはそのカラーに応じたものになります。この上昇色は次の方法で変更できます-

Style.xmlに移動し、次の色を変更します-

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

テキストの色を変更するにはセレクターを使用する必要があります `:

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

そして、textColorプロパティを@ drawable/selector_nameに設定します

0
rachit

このコードを使用して、その助けになるかもしれません

Android:textColorHighlight="your color code"
0
Pankaj Arora