EditText with TextWatcher and Suggestions

非 Y 不嫁゛ 提交于 2019-12-12 16:30:57

问题


Moving an app from 2.2 to 3.x, one of my EditText's that I was using a TextWatcher on for validation is behaving badly. In short, when a user clicks on the EditText and the entire word goes into 'suggestions mode' (where it is underlined), it effectively gets removed from the EditText from the TextWatcher's perspective, triggering my text validation check that I do for an empty EditText. The code:

mText = (EditText) findViewById(R.id.inpt_title);
mText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable editable) {
        final String title = editable.toString();
    Log.d(LOG_TAG, "addTextChangedListener(): title: " + title + ", length: " + title.length());
    if (title.length() == 0) {
    // empty title
    mText.setError(getString(R.string.error_note_title_empty));
    }
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    mText.setTextColor(getResources().getColor(R.color.black));
}

@Override
public void onTextChanged(CharSequence s, int start, int count, int after) {}
});

I'd like to keep suggestions working, but there seems to be some weird interaction here.

Any way to either a) keep the EditText from being empty when the entire word is in 'suggestion mode', or at least checking to see if the EditText is in the 'suggestion' state to determine if the EditText is truly empty, or b) turing off suggestions? I've tried android:inputType="text|textCapWords|textNoSuggestions" for the EditText in question, as well as setting it via mText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); in the code above but suggestions keep happening on a Lenovo 3.1 tablet.

Update:

I see API 14 added a isInputMethodTarget() method to the EditText, which I could use to check for active suggestions and disable the validation... but I am running against API 12. Perhaps I could check the IME directly to see if the suggestions are active?

来源:https://stackoverflow.com/questions/8595385/edittext-with-textwatcher-and-suggestions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!