在Android中更改文本高亮颜色

11

我不确定这是否可能,也许有人能帮我理清楚。我在一个 Android 应用程序中有一个 EditText 视图,其白色文本显示在蓝色背景上。当通过长按和编辑对话框选择文本时,我希望高亮显示为白色并将文本颜色更改为黑色。然而令人恼火的是,似乎没有办法设置高亮文本的颜色。您可以使用 textColorHighlight 设置高亮颜色,但无法设置文本颜色,因此将白色文本与白色高亮一起设置会导致一个大白块。

看起来这应该是可以通过在 XML 中声明性地进行的一些微小操作,但尽管我尝试了许多样式和颜色的组合,仍然无法更改颜色。

检查其他标准应用程序,似乎文本颜色从未发生变化,因此我认为这是不容易实现的。如果可能的话,我不想子类化 EditText,只是想做一些简单的事情。我有遗漏什么吗?可以在视图 XML 中完成此操作吗?

5个回答

2
尝试使用以下代码在您的主题样式中设定高亮文本颜色:<item name="android:textColorHighlight">your_color</item> 例如:
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">your_color1</item>
    <item name="colorPrimaryDark">your_color2</item>
    <item name="colorAccent">your_color3</item>
    <item name="android:textColor">your_color4/item>
    <item name="android:textColorHighlight">your_color</item>
</style>

然后在清单文件中将此样式用作活动主题。例如-

<activity
    android:name=".youractivity"
    android:label=""
    android:theme="@style/AppTheme.NoActionBar"       
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

2

我已经尝试了所有的 <EditText android:id="@+id/digits" android:textColorPrimary="@color/my_text_highlight" android:textColorSecondary="@color/my_text_highlight" android:textColorPrimaryInverse="@color/my_text_highlight" android:textColorSecondaryInverse="@color/my_text_highlight" /> 和一个样式 style="@style/myDisplayAppearance" 其中所有上述项目都被指定为项。遗憾的是,它们都没有成功。 - sgargan
就像我说的那样,你试过 android:textColor="?android:attr/textColorPrimaryInverse" 吗? - fedj
是的,我尝试了上面的方法,但它只是将默认文本颜色更改为textColorPrimaryInverse,而在我的情况下是黑色。我真的想让它默认为白色,只有在高亮时才变成黑色。什么时候会应用反色?顺便感谢您的帮助。 - sgargan
你应该看一下:http://developer.android.com/reference/android/R.attr.html 中的文本属性。 - fedj

0
Object mSelectionForegroundColorSpan;
@Override
protected void onSelectionChanged(int selStart, int selEnd) {
    super.onSelectionChanged(selStart, selEnd);
    if(mSelectionForegroundColorSpan == null){
        mSelectionForegroundColorSpan = new ForegroundColorSpan(Color.GREEN);
    }else{
        getText().removeSpan(mSelectionForegroundColorSpan);
    }
    if(selStart > selEnd){
        int swap = selStart;
        selStart = selEnd;
        selEnd = swap;
    }
    getText().setSpan(mSelectionForegroundColorSpan, selStart, selEnd, Spanned.SPAN_INTERMEDIATE);
}

重写 TextViewonSelectionChanged 方法,获取文本并设置 ForegroundColorSpan

enter image description here


0

正如this的Stack Overflow帖子所提到的,您应该使用选择器来更改文本颜色,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Pressed State -->
    <item android:state_pressed="true"
        android:color="#FFFFFF" />

    <!-- Focused State -->
    <item android:state_focused="true"
        android:color="#FFFFFF" />

    <!-- Default State -->
    <item android:color="#000000" />
</selector>

然后将您的textColor属性设置为@drawable/selector_name


0

这会改变高亮的颜色,而不是实际文本的颜色。 - k3v

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