在Android 5.0以下版本中,EditText的光标和指针颜色如何设置?

10

我想要改变EditText光标指针的颜色(从默认的蓝色变为白色),但是没有任何解决方案适用于我的项目。我已经尝试开发一个演示项目,同样的代码在那里运行良好。对于>=android 5.0,这段代码运行正常。

我不知道哪个属性被我的值覆盖了。我有两个选项来解决这个问题:

  1. 找到控制该颜色值的属性。
  2. 当页面加载时(在onViewCreated中)通过Java代码更改颜色值。

请建议如何解决这个问题。

styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="my_text_layout_style">
        
        <item name="android:textColor">#FFF</item>
        <item name="android:textColorHint">#FFF</item>
      
        <item name="colorAccent">#FFF</item>
        <item name="colorControlNormal">#FFF</item>
        <item name="colorControlActivated">#FFF</item>

        <item name="colorControlHighlight">#FFF</item>

    </style>

    

    <style name="my_edit_text_style">
        <item name="colorAccent">#FFF</item>
        <item name="android:editTextStyle">@style/MyEditTextStyle</item>
    </style>

    <style name="MyEditTextStyle" parent="Widget.AppCompat.EditText" />
</resources>

布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/base_disable_btn_bg_color"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:hint="just a hint"
        app:hintTextAppearance="@style/my_text_layout_style"
        android:theme="@style/my_text_layout_style"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:theme="@style/my_edit_text_style"
            android:layout_height="wrap_content" />

    </android.support.design.widget.TextInputLayout>
</LinearLayout>

尝试以编程方式添加视图,但没有成功

AppLinearLayout appLinearLayout = (AppLinearLayout) view.findViewById(R.id.some_id);
EditText editText = new EditText(new ContextThemeWrapper(getContext(), R.style.AppTheme_Cursor));
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(params);
appLinearLayout.addView(editText);

屏幕截图


尝试在color.xml中更改colorAccent颜色,然后告诉我它是否有效。 - Nainal
@JamesMacca 尝试了上面的答案,它只改变了光标颜色而不是指针颜色。 - Harish Gyanani
@HarishGyanani 哦,没问题。 - James McNee
https://dev59.com/7Jzha4cB1Zd3GeqPG46-#40642542 - Malik Abu Qaoud
@MalikAbuQaoud 我正在应用程序模块中使用它。我认为没有其他模块或文件可以覆盖它。 - Harish Gyanani
显示剩余2条评论
8个回答

3

试试这个链接。对我来说,它有效。

另外,你可以在这里找到并修改你的SDK上的drawablesAndroid\sdk\platforms\YOUR_ANDROID_VERSION\data\res at drawables-*dpi

你可以随意复制和修改它们的颜色/形状


2

尝试这个解决方案:

EditText中使用此属性:android:textCursorDrawable="@drawable/cursor_color"

drawable文件夹中创建:cursor_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<size android:width="1dp" />

<solid android:color="#c8c8c8" />


2

我使用了这个,它对我来说正常工作。

<style name="edittext" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorControlNormal">@color/gray</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:textColorHint">@color/gray</item>
</style>

<EditText
            android:id="@+id/email"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:gravity="center"
            android:hint="@string/email_address"
            android:inputType="textEmailAddress"
            android:padding="@dimen/padding_16"
            android:textColor="@color/white"
            android:textColorHint="@color/login_color"
            android:textCursorDrawable="@color/mdtp_white"
            android:theme="@style/edit" />

对我不起作用。在上面的代码中,无论何处使用颜色,都使用了#F00(红色)。 - Harish Gyanani

1
使用以下代码在您的EditText中添加属性: attribute
android:textCursorDrawable="@null"

1
尝试过了,不起作用。它用于更改光标颜色而不是指针颜色。 - Harish Gyanani

1
你可以为这个EditText创建自己的样式/主题,并更改ColorAccent
<style name="EditTextColorCustom" parent="@style/AppBaseTheme">
    <!-- Customize your theme here. -->
    <item name="colorAccent">@color/colorAccent</item>
</style>

将以下内容翻译成中文,保留HTML标签,不做解释:

在您的values-21文件夹中使用相同的style


values-21 是在5.0或以上版本中使用的,而我的问题出现在4.4或以下版本中。这该如何解决我的问题? - Harish Gyanani
不要误解Drawable-21或values-21是用于支持API级别低于21的。 - Saurabh Srivastava
请访问以下网址以了解有关Android兼容性的培训材料:https://developer.android.com/training/material/compatibility.html - Saurabh Srivastava
我已经在 valuesvalues-v21 文件夹中添加了这个样式,但结果仍然相同。正在 4.4 genymotion 模拟器上进行测试。 - Harish Gyanani

1
创建一个新的drawable。
这里是cursor.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <size android:width="2dp" />
    <solid android:color="#EF5350"/>
    <stroke android:color="#EF5350"/>
</shape>

将其翻译为中文:在 EditText 中使用它,就像这样:

 <EditText
      android:id="@+id/ed"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/roundedcornerwhite"
      android:hint="edit text"
      android:paddingBottom="10dp"
      android:paddingRight="10dp"
      android:paddingTop="10dp"
      android:maxLines="1"
      android:singleLine="true"
      android:imeOptions="actionNext"
      android:textColor="@color/colorPrimaryText"
      android:textColorHint="@color/hintcolor"
      android:textCursorDrawable="@drawable/cursor"
      android:textSize="16sp" />

更新:

只需像这样制作一个新主题 -

<style name="themex" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimaryDark">#666666</item>
        <item name="android:listDivider">@color/white_pressed</item>
        <item name="colorPrimary">@color/colorIcons</item>
        <item name="android:textColor">#b6b6b6</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="colorControlActivated">@color/olagreen</item>
        <item name="android:windowAnimationStyle">@null</item>

    </style>

这里的"colorcontrolactivated"将是您问题的答案。将此主题添加到您的活动中,并从您的布局中移除您提供给EditText的样式。

<activity
            android:name="youractivityname"
            android:parentActivityName="com.example.AboutUs"
            android:screenOrientation="portrait"
            android:theme="@style/themex"/>

更新:

如果对您不起作用,请检查您的片段是否在活动中,并且您为该活动提供了正确的主题。片段也将具有此主题。如果仍然无法正常工作,请尝试以下操作 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:id="@+id/mainlayout"
        style="@style/themex"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorIcons"
        android:orientation="vertical"
        android:weightSum="2">

    </RelativeLayout> 

我希望这可以帮助你。

它只是改变了光标颜色。指针颜色与之前相同(在图像中显示为深蓝色#192B3C)。 - Harish Gyanani
深蓝色是您的强调颜色,请相应更改 - Aman Verma
我想要更改EditText的强调颜色。我不想为整个应用程序更改强调颜色。 - Harish Gyanani
1
创建一个新的主题或样式,并将此样式添加到清单中的特定活动。 - Aman Verma
如果您的片段位于任何活动中,则它将使用相同的主题作为该片段,如果不起作用,则通过在布局的rootview中定义来为特定片段定义主题。 - Aman Verma
显示剩余5条评论

0

我结合了一些SO上的答案,找到了最好的方法:

  1. 在你的Android SDK文件夹中查找原始的drawable (\android-sdk\platforms\android-23\android.jar\res\drawable-hdpi-v4) enter image description here

  2. 使用编辑器编辑这些图像并将它们放入你的drawable文件夹中。

  3. 将以下行添加到你的样式中:

    <item name="android:textSelectHandle">@drawable/my_drawable_handle</item>
    <item name="android:textSelectHandleLeft">@drawable/my_drawable_handle_left</item>
    <item name="android:textSelectHandleRight">@drawable/my_drawable_handle_right</item>
    

结果:

enter image description here


0
<style name="my_edit_text_style">
    <item name="colorAccent">#FFF</item>
    <item name="android:editTextStyle">@style/MyEditTextStyle</item>
    <item name="colorAccent">#FFF</item>
    <item name="colorControlNormal">#FFF</item>
    <item name="colorControlActivated">#FFF</item>
</style>

这可能会对你有所帮助。


你在那里重复了一行 <item name="colorAccent">#FFF</item> - Micer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接