【问题标题】:EditText cursor and pointer color for below android 5.0android 5.0以下的EditText光标和指针颜色
【发布时间】:2017-05-05 12:52:19
【问题描述】:

我正在尝试更改 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>

layout.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 颜色并让我知道它是否有效
  • @JamesMacca 尝试了上面的答案,它只改变光标颜色而不是指针颜色
  • @Nainal 检查我的代码,我试过了。
  • @HarishGyanani 啊,没问题。
  • 从最新的 android studio 版本中,style.xml 有两个文件夹,你有没有在这两个文件夹中定义你的风格。一个是style另一个是style-V21

标签: android android-edittext android-textinputedittext


【解决方案1】:

试试这个link。对我来说,它奏效了。 而且,您可以在SDK 上找到并修改drawablesAndroid\sdk\platforms\YOUR_ANDROID_VERSION\data\res at drawables-*dpi

您可以随意copy and modify他们的color/form

【讨论】:

    【解决方案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" />
    

    【讨论】:

      【解决方案3】:

      在您的EditText 中使用此attribute

      android:textCursorDrawable="@null"
      

      【讨论】:

      • 试过了,没用。它用于更改光标颜色而不是指针颜色。
      【解决方案4】:

      您可以仅为此EditText 创建自己的style/theme 并更改ColorAccent

      <style name="EditTextColorCustom" parent="@style/AppBaseTheme">
          <!-- Customize your theme here. -->
          <item name="colorAccent">@color/colorAccent</item>
      </style>
      

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

      【讨论】:

      • values-21 用于 5.0 或更高版本,我的问题出现在 4.4 或更低版本。这将如何解决我的问题?
      • 不要误解 Drawable-21 或 values-21 用于支持 api 级别 21。
      • 我已经在valuesvalues-v21 文件夹中添加了这个样式。仍然得到相同的结果。在 4.4 genymotion 模拟器上测试。
      【解决方案5】:

      我用过这个,它对我很有效。

      <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(红色)颜色。
      【解决方案6】:

      创建一个新的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)。
      • 深蓝色是你的强调色,相应地改变它
      • 我想改变edittext的强调色。我不想更改整个应用的强调色。
      • 创建一个新的主题或样式并将这个样式添加到清单中的特定活动中
      • 请检查我已经这样做了。如果您在我的代码中发现任何问题,请告诉我。
      【解决方案7】:

      我结合了几个关于 SO 的答案并找到了最好的方法:

      1. 在您的 android sdk 文件夹中查找原始可绘制对象 (\android-sdk\platforms\android-23\android.jar\res\drawable-hdpi-v4)

      2. 使用编辑器编辑这些图像并将它们放入您的可绘制文件夹中。

      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>
        

      结果:

      【讨论】:

        【解决方案8】:
        <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>
        

        这可能会对你有所帮助。

        【讨论】:

        • 你在&lt;item name="colorAccent"&gt;#FFF&lt;/item&gt;那里重复了一行。
        猜你喜欢
        • 2016-06-05
        • 2017-12-26
        • 2016-02-29
        • 2011-12-25
        • 1970-01-01
        • 2017-11-25
        • 2012-07-23
        • 2011-11-06
        相关资源
        最近更新 更多