android - 删除 EditText 中的附加下划线

标签 android android-layout android-edittext

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

enter image description here

编辑文本代码:

<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 数据绑定(bind)库和 MVVM 架构。

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

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

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

@drawable/profile_et_background_active代码:

<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代码:

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

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

enter image description here

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

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

但它不起作用。

如何删除多余的下划线?

更新 1

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

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

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

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

更新 2

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

最佳答案

下划线文本样式由 BaseInputConnection 应用的 EditText使用主题属性 android:candidatesTextStyleSpans 应用的样式到当前正在“组合”的文本,默认设置为字符串 <u>candidates</u> .

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

您可以更改该样式(例如,使用粗体或斜体而不是下划线),或者通过将 theme 属性设置为带样式或无样式的字符串来完全删除样式:

<!-- 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>为默认文本颜色,这在显示上没有区别。

关于android - 删除 EditText 中的附加下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47831577/

相关文章:

android - 如何在 android 中为底部导航栏设置顶部边框,如图所示

android - 仅将粗体文本保存到跨字符串中的字符串数组?

java - 在开始 Activity 之前需要单击“下一步”按钮两次

android - APK 解析错误

android - 如何实现 Android 日历应用程序的 Day-View?

android - 在android中支持多种屏幕尺寸和旋转

android - 如何在android通知中增加remoteview的高度

android - 如何在 android firebase 聊天中显示打字指示器

android - 如何在libPd中播放WAV文件?

android - 与听众打交道