【问题标题】:Changing Android's EditText cursor pointer color programmatically以编程方式更改 Android EditText 光标指针颜色
【发布时间】:2016-06-05 15:09:00
【问题描述】:

所以,我搜索了很多,但我找不到任何东西,如果这最终是重复的,我很抱歉,但我想改变用于移动光标的 EditText 光标指针的颜色

我已经设法改变了光标本身的颜色,但它不会影响似乎只响应 AccentColor 的指针。

我很确定这是可能的,因为我记得看到一个应用程序做到了,它有指针,对话框中的所有元素都会根据您选择的颜色更改颜色,而不会更改视图下方视图的强调色对话框。

请帮忙:)

【问题讨论】:

  • 如你所说,我认为你可以通过改变accentColor来改变这个颜色
  • 是的,但是改变强调色也会改变被硬连线到强调色的底层元素的颜色
  • 也许您可以为此编辑文本创建一个自定义主题并根据需要设置accentColor
  • 我没想到这一点,我马上试试,告诉你,你知道以编程方式更改主题的强调色的方法吗?
  • 可以检查这个:stackoverflow.com/questions/25354558/…,但似乎并不像只在styles.xml中添加几行那么简单:)

标签: android pointers colors android-edittext


【解决方案1】:

您可以仅为此 EditText 创建自己的样式/主题并更改 ColorAccent :

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

【讨论】:

    【解决方案2】:

    您可以像这样在 style.xml 中创建新样式来做到这一点

    <style name="CustomEditTextTheme" parent="TextAppearance.AppCompat">
        <item name="colorAccent">@color/primary_dark_material_dark</item>       
    </style>
    

    然后在 EditText 标签中使用这个

    style="@style/CustomEditTextTheme"
    

    【讨论】:

    • 我写的是android:theme="@style/CustomEditTextTheme",而不是style=...。
    • &lt;item name="colorControlActivated"&gt;@color/primary_dark_material_dark&lt;/item&gt; 如果colorAccent 不起作用,请改用它
    【解决方案3】:

    您可以使用 textSelectHandle 将其替换为您的可绘制对象,如下所示:

    android:textSelectHandle="@drawable/your_drawble"
    

    【讨论】:

    • 没有一个建议的变体适用于我的 TextInputEditText 或 AppCompatAutoCompleteTextView,只有这个。我使用了 Android Studio 图标中的“invert_colors”矢量
    【解决方案4】:

    您可以使用反射来设置 selectHandle。

    EditText 具有具有 mSelectHandleCenter 字段的 Editor 类(如果您需要选择使用 mSelectHandleLeft 和 mSelectHandleRight)

    private fun setCursorPointerColor(view: EditText, @ColorInt color: Int) {
        try {
            //get the pointer resource id
            var field = TextView::class.java.getDeclaredField("mTextSelectHandleRes")
            field.isAccessible = true
            val drawableResId = field.getInt(view)
    
            //get the editor
            field = TextView::class.java.getDeclaredField("mEditor")
            field.isAccessible = true
            val editor = field.get(view)
    
            //tint drawable
            val drawable = ContextCompat.getDrawable(view.context, drawableResId)!!
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN)
    
            //set the drawable
            field = editor.javaClass.getDeclaredField("mSelectHandleCenter")
            field.isAccessible = true
            field.set(editor, drawable)
    
        } catch (ex: Exception) {
        }
    }
    

    【讨论】:

      【解决方案5】:

      您可以将 colorControlActivated 添加到您的 AppTheme 样式中:

      &lt;item name="colorControlActivated"&gt;"Your_color"&lt;/item&gt;

      【讨论】:

      • 这确实有效.. 但我认为它会覆盖颜色强调设置
      猜你喜欢
      • 2014-11-17
      • 2015-04-02
      • 2016-02-29
      • 2017-05-05
      • 2017-04-14
      • 2012-07-23
      • 1970-01-01
      • 2017-11-25
      • 2017-12-26
      相关资源
      最近更新 更多