带有 TextWatcher 和建议的 EditText

发布于 2024-12-22 09:32:18 字数 1410 浏览 5 评论 0原文

将应用程序从 2.2 移动到 3.x 时,我使用 TextWatcher 进行验证的 EditText 之一表现不佳。简而言之,当用户单击 EditText 并且整个单词进入“建议模式”(带有下划线)时,从 TextWatcher 的角度来看,它实际上会从 EditText 中删除,从而触发我的文本验证检查我是否对空的 EditText 执行了操作。代码:

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) {}
});

我想让建议继续有效,但这里似乎有一些奇怪的交互。

任何方法 a) 当整个单词处于“建议模式”时保持 EditText 不为空,或者至少检查 EditText 是否处于“建议”状态以确定 EditText 是否真正为空,或者 b )关闭建议?我已经尝试过 android:inputType="text|textCapWords|textNoSuggestions" 对于有问题的 EditText,并通过 mText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); 设置它在上面的代码中,但建议在 Lenovo 3.1 平板电脑上不断出现。

更新:

我看到 API 14 在 EditText 中添加了一个 isInputMethodTarget() 方法,我可以用它来检查活动建议并禁用验证...但我正在针对 API 12 运行。也许我可以直接检查 IME 以查看建议是否有效?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文