问题
I have EditText with custom background drawable:
EditText code:
<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" />
I'm using android databinding library and MVVM architecture.
If ViewModel has isAllowEdit set to true than EditText background set to @drawable/profile_et_background_active.
If isAllowEdit false EditText has background set to @drawable/profile_et_background.
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 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 code:
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/transparent" />
</shape>
</item>
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):
I tried to remove that underline by adding color filter to EditText:
et.setColorFilter(getResources().getColor(android.R.color.transparent), PorterDuff.Mode.SRC_IN)
But it doesn't work.
How can i remove that extra underline ?
UPDATE 1
I already tried to add @android:color/transparent, and I'm getting error:
"java.lang.Integer cannot be cast to android.graphics.drawable.Drawable"
when changing "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @drawable/profile_et_background}"
to "@{ViewModel.isAllowEdit ? @drawable/profile_et_background_active : @android:color/transparent}"
UPDATE 2
Adding InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS does not work for me. So i guess this is not Spell Checker's problem.
回答1:
You can set the EditText to have a custom transparent drawable or just use
android:background="@android:color/transparent".
回答2:
I have seen all the valid answers given above. But at the last try You can something like:
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);
}
});
ClearComposingText method will help you. Hope this helps.
回答3:
Use inputType textNoSuggestions in xml.
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text|textNoSuggestions"/>
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS is equivalent flag for this using code
回答4:
As per my understanding. Use this in your editText
android:background="@android:color/transparent"
And If you want to stop Spell Checker for Text which you had typed then use
android:inputType="textNoSuggestions"
来源:https://stackoverflow.com/questions/47831577/remove-additional-underline-in-edittext