删除 EditText 中的附加下划线

remove additional underline in EditText(删除 EditText 中的附加下划线)

本文介绍了删除 EditText 中的附加下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有自定义背景可绘制的 EditText:

I have EditText with custom background drawable:

编辑文本代码:

<EditText
    android:id="@+id/etName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@{ViewModel.isAllowEdit  ? @drawable/profile_et_background_active : @drawable/profile_et_background}"
    android:inputType="@{ViewModel.isAllowEdit ? InputType.TYPE_CLASS_TEXT : InputType.TYPE_NULL}"
    android:text="@={ViewModel.name}"
    android:textColor="@color/main_dark_text_color" />

我正在使用 android 数据绑定库和 MVVM 架构.

I'm using android databinding library and MVVM architecture.

如果 ViewModel 将 isAllowEdit 设置为 true,而不是将 EditText 背景设置为 @drawable/profile_et_background_active.

If ViewModel has isAllowEdit set to true than EditText background set to @drawable/profile_et_background_active.

如果 isAllowEdit false EditText 的背景设置为 @drawable/profile_et_background.

If isAllowEdit false EditText has background set to @drawable/profile_et_background.

另外我通过将 inputType 设置为 TYPE_NULL 来禁止编辑,并通过将 inputType 设置为 TYPE_CLASS_TEXT 来允许编辑.

Also i'm disallow edit by setting inputType to TYPE_NULL, and allow edit by setting inputType to TYPE_CLASS_TEXT.

@drawable/profile_et_background_active 代码:

@drawable/profile_et_background_active code:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>

    <item
        android:left="-2dp"
        android:right="-2dp"
        android:top="-2dp">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:width="1dp"
                android:color="@color/main_elements_line_color" />
        </shape>
    </item>

</layer-list>

@drawable/profile_et_background 代码:

@drawable/profile_et_background code:

<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/transparent" />
    </shape>
</item>

当允许编辑并且用户开始在 EditText 中输入文本时,输入的单词下方会出现额外的下划线(它只属于当前输入的单词,EditText 文本的所有其他部分都没有下划线):

When edit is allowed and user start typing text in EditText additional underline appears under typed word (it belongs only to currently typed word, all other parts of EditText text has no underline):

我试图通过向 EditText 添加颜色过滤器来删除该下划线:

I tried to remove that underline by adding color filter to EditText:

et.setColorFilter(getResources().getColor(android.R.color.transparent), PorterDuff.Mode.SRC_IN)

但它不起作用.

如何去除多余的下划线?

How can i remove that extra underline ?

更新 1

我已经尝试添加@android:color/transparent,但出现错误:

I already tried to add @android:color/transparent, and I'm getting error:

java.lang.Integer 无法转换为 android.graphics.drawable.Drawable"

"java.lang.Integer cannot be cast to android.graphics.drawable.Drawable"

当更改@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @drawable/profile_et_background}"时

when changing "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @drawable/profile_et_background}"

到@{ViewModel.isAllowEdit ?@drawable/profile_et_background_active : @android:color/transparent}"

to "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @android:color/transparent}"

更新 2

添加 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS 对我不起作用.所以我想这不是拼写检查的问题.

Adding InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS does not work for me. So i guess this is not Spell Checker's problem.

推荐答案

下划线文本样式由 EditTextBaseInputConnection 应用于当前正在合成"的文本;使用主题属性 android:candidatesTextStyleSpans 应用的样式,默认情况下设置为字符串 <u>candidates</u>.

The underline text styling is applied by the BaseInputConnection of EditText to the text currently being "composed" using the styling applied by the theme attribute android:candidatesTextStyleSpans, which by default is set to the string <u>candidates</u>.

字符串的文本部分被忽略,但样式跨度从字符串中提取并应用于组合".text 是用户当前正在输入的单词,a.o.表示可以选择建议或自动更正处于活动状态.

The text part of the string is ignored, but the style spans are extracted from the string and applied to "composing" text which is the word the user is currently typing, a.o. to indicate that suggestions can be selected or that autocorrect is active.

您可以更改样式(例如,使用粗体或斜体代替下划线),或完全删除样式,方法是将主题属性设置为样式化或非样式化字符串:

You can change that styling (e.g. to use bold or italics instead of underlines), or remove the styling altogether, by setting the theme attribute to a styled or unstyled string:

<!-- remove styling from composing text-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- ... -->
    <item name="android:candidatesTextStyleSpans">candidates</item>
</style>

<!-- apply bold + italic styling to composing text-->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- ... -->
    <item name="android:candidatesTextStyleSpans"><b><i>candidates</i></b></item>
</style>

警告:删除所有样式将导致 BaseInputConnection 实现在每次更改文本时重新评估主题属性,因为跨度信息是延迟加载的,并且仅当属性设置为样式字符串时才会保留.您也可以设置 Html:fromHtml(...) 支持的任何其他样式,例如<span style="color:#000000">...</span> 为默认的文本颜色,这在显示上没有区别.

Caveat: Removing all styling will cause the BaseInputConnection implementation to re-evaluate the theme attribute on every change of text, as the span information is lazy loaded and persisted only if the attribute is set to a styled string. You could alternatively set any other styling as is supported by Html:fromHtml(...), e.g. <span style="color:#000000">...</span> to the default text color, which makes no difference in display.

这篇关于删除 EditText 中的附加下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:删除 EditText 中的附加下划线

基础教程推荐