問題描述
我有帶有自定義背景可繪制的 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.
推薦答案
下劃線文本樣式由 EditText
的 BaseInputConnection
應用于當前正在合成"的文本;使用主題屬性 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 中的附加下劃線的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!