【问题标题】:How to change color / appearance of EditText select handle / anchor?如何更改 EditText 选择句柄/锚点的颜色/外观?
【发布时间】:2013-02-14 13:10:30
【问题描述】:

所以我将带有Holo Colors GeneratorAction Bar Style Generator 的Holo Theme 的样式更改为我自己的颜色。但是当我在编辑文本中选择文本时,所选位置的“标记”仍然是蓝色的。怎么改?

【问题讨论】:

标签: android android-edittext android-theme


【解决方案1】:

这里最糟糕的部分是找到该项目的“名称”以及如何在主题中调用它。所以我查看了android SDK文件夹中的每个drawable,最后找到了名为“text_select_handle_middle”、“text_select_handle_left”和“text_select_handle_right”的drawable。

因此解决方案很简单:将这些具有自定义设计/颜​​色的可绘制对象添加到您的可绘制文件夹中,并将它们添加到您的主题样式定义中,例如:

<style name="MyCustomTheme" parent="@style/MyNotSoCustomTheme">
        <item name="android:textSelectHandle">@drawable/text_select_handle_middle</item>
        <item name="android:textSelectHandleLeft">@drawable/text_select_handle_left</item>
        <item name="android:textSelectHandleRight">@drawable/text_select_handle_right</item>
</style>

【讨论】:

  • 聚会有点晚了,但请参阅下面关于如何以编程方式执行此操作的答案。 @蚂蚁
  • 即使只应用于一个 EditText 或 TextView,它也可以工作,而不仅仅是应用程序范围。
  • 只是一个友好的提醒。应用此解决方案后,editView 可能会在某些三星设备上失去响应。我们刚刚找到它,检查here
【解决方案2】:

我知道这真的很晚了,但是如果您只想更改句柄的颜色,您只需将以下内容添加到您的 styles.xml 文件中。

<style name="ColoredHandleTheme">
    <item name="colorControlActivated">@color/colorYouWant</item>
</style>

然后只需将主题设置为持有您想要影响的EditText 的任何活动。

或者,如果您想在应用范围内设置它,您可以执行以下操作:

<style name="ColoredHandleThemeForWholeApp">
    <item name="colorAccent">@color/colorYouWant</item>
</style>

并为整个应用设置该主题。

问题解决了!

【讨论】:

  • 我认为 colorControlActivated 直接设置为 EditText 时不起作用。仅适用于活动级别(通过清单)。在搭载 Android 6 的 Nexus 5 和搭载 Android 4.4.4 的 Nexus 4 上测试。
  • 可能是对的。我没有说它可以在单个 EditText 上工作。我说你可以在活动或应用程序范围内设置它。
  • 哦,好的。对不起,谢谢。那你知道这样的解决方案吗?
  • 我不抱歉。为什么要在同一活动中的不同 EditText 上使用不同的样式?
  • 因为如果只有一个 EditText 有显示其文本选择句柄的问题(由于它背后的背景),这并不意味着所有 EditText 都有这个问题,所以它没有意义更改所有样式的样式。只为我感兴趣的那个。
【解决方案3】:

如何从代码中做到这一点:

try {
    final Field fEditor = TextView.class.getDeclaredField("mEditor");
    fEditor.setAccessible(true);
    final Object editor = fEditor.get(editText);

    final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
    final Field fSelectHandleRight =
        editor.getClass().getDeclaredField("mSelectHandleRight");
    final Field fSelectHandleCenter =
        editor.getClass().getDeclaredField("mSelectHandleCenter");

    fSelectHandleLeft.setAccessible(true);
    fSelectHandleRight.setAccessible(true);
    fSelectHandleCenter.setAccessible(true);

    final Resources res = context.getResources();

    fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.text_select_handle_left));
    fSelectHandleRight.set(editor, res.getDrawable(R.drawable.text_select_handle_right));
    fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.text_select_handle_middle));
} catch (final Exception ignored) {
}

【讨论】:

  • 是的,这是可能的方法之一。但反思不是个好主意)
  • 如果我只想更改“mSelectHandleCenter”的颜色而不是用新的drawable替换它怎么办?我怎样才能做到这一点?不幸的是,我不得不使用反射,因为我通过我们的 API 获得了新的“强调色”
  • @nicopasso, gist.github.com/jaredrummler/2317620559d10ac39b8218a1152ec9d4 您可以通过删除两个数组中的左右字段名称将该方法更改为仅对中心句柄进行着色。
  • @JaredRummler 对于 text_select_handle_* 文件,您的可绘制文件是什么样的?
  • 这可以从线程中完成吗?我可以通过片段的 onCreateView 来实现它,但不能通过线程来实现。有什么线索吗?
【解决方案4】:

要更改选择手柄的颜色,您必须在应用主题中覆盖激活的颜色:

<style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="android:colorControlActivated">@color/customActivatedColor</item>
</style>

【讨论】:

  • 有什么办法让它只适用于活动中的特定 EditText,而不是全部?
  • 当然,您可以通过在布局中声明它们以使用您的自定义样式来将此样式仅用于特定的 EditText。
  • 它在特定的 EditText 上对我不起作用。仅在活动级别(使用清单)。可以试试吗?
【解决方案5】:

您可以在http://androiddrawables.com/Other.html 中看到这些属性。

更改你的 values/styles.xml,例如:

<style name="AppTheme.Cursor" parent="AppTheme">
    <item name="colorAccent">@color/cursor</item>
</style>

@color/cursor 添加在 values/color.xml 中。之后将样式应用于活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(R.style.AppTheme_Cursor);
    ...

访问How to change EditText pointer color (not cursor)了解其他解决方案。

【讨论】:

  • 这不起作用。在搭载 Android 6 的 Nexus 5 和搭载 Android 5 的 Nexus 4 上进行了测试。
  • @androiddeveloper,我现在不能说。我在运行 Android 5 的三星 Galaxy Tab A 上测试了这段代码。现在我认为其他解决方案更可取,因为它们不会更改 colorAccent(这可能会影响其他控件)。
【解决方案6】:

对于那些尝试通过代码处理此问题的人,您可以使用 Jared Rummler 提供的 API 版本小于或等于 28 的答案,但从 Android Q(API 29) 开始,他们添加了 @UnsupportedAppUsage(maxTargetSdk = Build. VERSION_CODES.P) 注释,它似乎不适用于 Q+。而是添加到 EditText 的新方法:

setTextSelectHandle(R.drawable.test);
setTextSelectHandleLeft(R.drawable.test);
setTextSelectHandleRight(R.drawable.test);

因此,要使其正常工作,您可以执行以下操作:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                setTextSelectHandle(R.drawable.test);
                setTextSelectHandleLeft(R.drawable.test);
                setTextSelectHandleRight(R.drawable.test);
            } else {
                try {
                    final Field fEditor = TextView.class.getDeclaredField("mEditor");
                    fEditor.setAccessible(true);
                    final Object editor = fEditor.get(this);

                    final Field fSelectHandleLeft = editor.getClass().getDeclaredField("mSelectHandleLeft");
                    final Field fSelectHandleRight =
                            editor.getClass().getDeclaredField("mSelectHandleRight");
                    final Field fSelectHandleCenter =
                            editor.getClass().getDeclaredField("mSelectHandleCenter");

                    fSelectHandleLeft.setAccessible(true);
                    fSelectHandleRight.setAccessible(true);
                    fSelectHandleCenter.setAccessible(true);

                    final Resources res = getResources();

                    fSelectHandleLeft.set(editor, res.getDrawable(R.drawable.test, null));
                    fSelectHandleRight.set(editor, res.getDrawable(R.drawable.test, null));
                    fSelectHandleCenter.set(editor, res.getDrawable(R.drawable.test, null));
                } catch (final Exception ignored) {
                }
            }

【讨论】:

    【解决方案7】:

    如果您只想在一个 EditText 上隐藏指针,您可以只为其设置主题:

    styles.xml

    <style name="TransparentPointerEditText">
        <item name="colorControlActivated">@android:color/transparent</item>
    </style>
    

    用法

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/TransparentPointerEditText" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2020-10-02
      • 2021-04-20
      • 1970-01-01
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多