EditText: Toggle InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

I created a custom EditText by extending android.widget.EditText .

I would like auto-correct intervals to be visible only when EditText has focus. Therefore, I call setInputType(INPUT_NO_FOCUS); in the constructor and:

 @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(focused, direction, previouslyFocusedRect); if (focused) { setInputType(INPUT_FOCUS); } else { setInputType(INPUT_NO_FOCUS); } } 

with:

 private final int INPUT_NO_FOCUS = TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; private final int INPUT_FOCUS = TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_MULTI_LINE | InputType.TYPE_TEXT_FLAG_AUTO_CORRECT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLET; 

==> Actually there is no automatic correction and automatic completion, but it does not appear after clicking on EditText . Input type successfully changed.

How to enable automatic correction / add autofill?

// Change:

User Case I want to archive:

  • MyEditText extends EditText displayed and not focused. There is no spell check in the text (-> There are no red spaces / lines below the text)

  • The user clicks in the edit box. β†’ Spell check starts β†’ Red lines appear (user can click misspelled words and let the OS fix them)

  • The user clicks on another View element β†’ Red lines / spaces disappear.

+4
source share
2 answers

I am afraid there is no good way to do this. The problem is that on the Android platform there is actually no mechanism to tell the keyboard when the input type has changed. Typically, the keyboard checks the type when opening a field. After that, if the type changes, he will not know if he does not interrogate it, and I do not think that any existing keyboard does this.

+2
source

Cancel this method in class MyEditText

 @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); setInputType(INPUT_FOCUS); } 

With this method, you can change the inputType for EditText to INPUT_FOCUS, which displays a red line below the text when the user edits the text MyEditText

0
source

All Articles