Vla*_*nov 5 android android-layout android-edittext
我有带有自定义背景可绘制的 EditText:
编辑文本代码:
<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" />
Run Code Online (Sandbox Code Playgroud)
我正在使用 android 数据绑定库和 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>
Run Code Online (Sandbox Code Playgroud)
@drawable/profile_et_background 代码:
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
当允许编辑并且用户开始在 EditText 中键入文本时,在键入的单词下会出现附加下划线(它仅属于当前键入的单词,EditText 文本的所有其他部分都没有下划线):
我试图通过向 EditText 添加颜色过滤器来删除该下划线:
et.setColorFilter(getResources().getColor(android.R.color.transparent), PorterDuff.Mode.SRC_IN)
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
我怎样才能删除那个额外的下划线?
更新 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 对我不起作用。所以我想这不是拼写检查器的问题。
下划线文本样式由BaseInputConnectionof应用于EditText当前正在“组合”的文本,使用主题属性应用的样式android:candidatesTextStyleSpans,默认情况下设置为 string <u>candidates</u>。
字符串的文本部分被忽略,但样式跨度从字符串中提取并应用于“撰写”文本,即用户当前键入的单词,ao 以指示可以选择建议或自动更正处于活动状态。
您可以更改样式(例如使用粗体或斜体而不是下划线),或通过将主题属性设置为样式或非样式字符串来完全删除样式:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
警告:删除所有样式将导致 BaseInputConnection 实现在每次更改文本时重新评估主题属性,因为只有在将属性设置为样式字符串时才会延迟加载和持久化跨度信息。您也可以将 支持的任何其他样式设置为Html:fromHtml(...),例如<span style="color:#000000">...</span>,设置为默认文本颜色,这对显示没有影响。
我已经看到了上面给出的所有有效答案。但是在最后一次尝试时,您可以执行以下操作:
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mEditText.clearComposingText();
}
},200);
}
});
Run Code Online (Sandbox Code Playgroud)
ClearComposingText 方法将帮助您。希望这可以帮助。
据我的理解。在你的 editText 中使用它
android:background="@android:color/transparent"
Run Code Online (Sandbox Code Playgroud)
如果您想停止对您输入的文本进行拼写检查,请使用
android:inputType="textNoSuggestions"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9179 次 |
| 最近记录: |