how to set cursor background transparent for EditText Android

血红的双手。 提交于 2019-12-23 10:49:14

问题


I'm having this issue when I touch an EditText for changing text on Android:


A white frame appears around the red cursor, and I need it to be transparent for showing properly the line of the EditText. How do I change this?

code:

<EditText
            android:id="@+id/login_user_name_text"
            android:textCursorDrawable="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:inputType="textEmailAddress"
            android:imeOptions="actionNext"
            android:maxLines="1"
            android:hint="@string/email"
            android:maxLength="60"
            />

EDIT: I could fix it by replacing:

<item name="android:background">@color/white</item>

for

<item name="android:background">@color/transparent</item>

in styles.xml

 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:background">@color/transparent</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>


回答1:


Sorry for the late,
I think you have
<item name="android:popupBackground">@color/white</item> in your style from v21,
just remove it, It may solve your problem




回答2:


better using @android:color/transparent instead of null on the android:textCursorDrawable="@android:color/transparent"




回答3:


Add the following to your EditText XML instead of null;

android:textCursorDrawable="@drawable/colorcursor"

Then, create a file called colorcursor.xml inside your drawable folder;

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



回答4:


Setting android:background to transparent didn't do the trick for me.

In my case I set a theme to the parent viewgroup that defined the background to be a certain color, so it was applied to the EditText as well. Making sure that the background was just set for the parent solved it.




回答5:


Another solution, which works if you are having this problem with an EditText inside a SearchView while using a Toolbar.

  1. In your activity XML add the android:background="?attr/colorPrimary" attribute to Toolbar:

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/ToolBarStyle"
    android:background="?attr/colorPrimary"
    android:elevation="2dp" />
    
  2. Then, remove <item name="android:background">@color/colorPrimary</item> from your ToolBarStyle in styles.xml.

Hope this helps someone else looking for a solution for this particular case.



来源:https://stackoverflow.com/questions/41232652/how-to-set-cursor-background-transparent-for-edittext-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!