Java 如何更改editText字段上的突出显示颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20639594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:17:37  来源:igfitidea点击:

how to change highlight color on editText field

javaandroidandroid-layoutandroid-edittext

提问by erik

Below is a screen shot of a custom edit text field i have in my android app. While you are type a given word, the text for the word you are currently typing highlights in grey and shows the text as black, until you hit the space bar at which time the text turns white as expected. Is there a way to change the color of the highlight and the text that is highlighted?

下面是我在我的 android 应用程序中的自定义编辑文本字段的屏幕截图。当您键入给定的单词时,您当前键入的单词的文本会以灰色突出显示,并将文本显示为黑色,直到您按空格键,此时文本会按预期变为白色。有没有办法更改突出显示的颜色和突出显示的文本?

enter image description here

在此处输入图片说明

my edit text xml looks like this

我的编辑文本 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>

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

回答by rachit

you should use a selector to change text colors`:

您应该使用选择器来更改文本颜色`:

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

And then set your textColor property to @drawable/selector_name

然后将你的 textColor 属性设置为 @drawable/selector_name

回答by Dreagen

You should use android:textColorHighlightin the EditText element in you layout xml file

您应该android:textColorHighlight在布局 xml 文件中的 EditText 元素中使用

回答by Kirk

Simple, just use TextHightLight Property in XML Mode

简单,只需在 XML 模式下使用 TextHightLight 属性

android:textColorHighlight="#ff0000"

or add this in your theme

或将此添加到您的主题中

<item name="android:textColorHighlight">"#ff0000"</item>

[Image attached here is because for a comment below]

[这里附上的图片是因为下面的评论]

and when i do, this is how it looks

当我这样做时,这就是它的样子

enter image description here

在此处输入图片说明

回答by Pankaj Arora

use this code,might be its help you

使用此代码,可能对您有所帮助

android:textColorHighlight="your color code"

回答by l-l

I was able to change the highlight of my edit text by adding this line to my themes.xml file:

通过将此行添加到我的 themes.xml 文件中,我能够更改编辑文本的突出显示:

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

回答by Mridul Shrivastava

In material design you will get the default ascent color in style so the all text selection and other theme would be according to that color. You can change this ascent color in following way -

在材料设计中,您将获得样式中的默认上升颜色,因此所有文本选择和其他主题都将根据该颜色。您可以通过以下方式更改此上升颜色 -

Go to the style.xml and change the following color -

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

回答by Abhijeet

And if you have to change the Marker handle then you should change

如果您必须更改标记句柄,那么您应该更改

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

回答by Cristan

In code:

在代码中:

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