Android:使用 TextWatcher 删除 EditText 中的字符仍然显示建议中的字符

发布于 2024-12-06 05:50:55 字数 1402 浏览 1 评论 0原文

在我的活动中,我有一个 EditText 来捕获文件名。我正在使用 TextWatcher 来防止用户输入我不希望他们在文件名中使用的某些字符。本质上我只希望用户输入以下字符:[a-zA-Z_0-9]。

@Override
public void afterTextChanged(Editable text) {
    String textStr = text.toString();
    int length = text.length();

    if (!Pattern.matches("\\w*", textStr)) {
        text.delete(length-1, length);
    }
}

编辑:添加更多代码 onCreate(...) 中

fileNameEditText = (EditText)findViewById(R.id.UploadPhoto_fileNameEditText);
fileNameEditText.addTextChangedListener(this);

在布局 xml 文件中的

<EditText
 android:id="@+id/UploadPhoto.fileNameEditText"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginBottom="20sp"
 android:layout_marginRight="10sp"
 android:layout_toRightOf="@id/UploadPhoto.fileNameLabel"/>

这可以完美地防止用户输入“\”和“.”之类的内容。我遇到的问题是,如果他们输入这些字符,它们就会显示在单词建议框中。这有点烦人,因为如果您尝试使用退格键删除字符,它会首先从建议中删除(即使该字符没有显示在 EditText 框中)。

如何防止不需要的字符出现在单词建议框中?

请参阅下面的屏幕截图。请注意,“-”(连字符)出现在建议框中,但不在 EditText 中。另请注意,建议框中连字符后还有另一个有效字符,该字符也不会显示在 EditText 中。这实际上会阻止用户输入更多文本,直到删除连字符,即使它不在 EditText 中。

更新:出现同样的问题,可以通过使用 InputFilter 而不是 TextWatcher 来重现。

更新:我想澄清一下,我的目标不是完全压制这些建议。问题是,当您阻止特定字符出现在 EditText 中时,它们仍然会显示在建议中。我的目标(赏金的目的)是防止相同的特定字符出现在建议中。

enter image此处描述

In my activity I have an EditText to capture a file name. I am using a TextWatcher to prevent users from entering certain characters that I don't want them to use in their filename. Essentially I only want users to enter in the following characters: [a-zA-Z_0-9].

@Override
public void afterTextChanged(Editable text) {
    String textStr = text.toString();
    int length = text.length();

    if (!Pattern.matches("\\w*", textStr)) {
        text.delete(length-1, length);
    }
}

EDIT: Adding more code
in onCreate(...)

fileNameEditText = (EditText)findViewById(R.id.UploadPhoto_fileNameEditText);
fileNameEditText.addTextChangedListener(this);

in layout xml file

<EditText
 android:id="@+id/UploadPhoto.fileNameEditText"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginBottom="20sp"
 android:layout_marginRight="10sp"
 android:layout_toRightOf="@id/UploadPhoto.fileNameLabel"/>

This works perfectly by preventing users from entering in things like "\" and ".". The problem that I'm having is that if they type these characters they show up in the word suggestions box. Its kind of annoying because if you try to delete a character using backspace, it deletes from the suggestion first (even though the character doesn't show up in EditText box).

How do you prevent the unwanted characters from showing up in the word suggestiong box?

See screen shot below. Notice that the "-" (hyphen) appears in the suggestion box, but not in the EditText. Also notice that there is another valid character in the suggestion box after the hyphen that also does not show up in the EditText. This essentially blocks the user from entering in more text until they delete the hyphen, even though its not in the EditText.

UPDATE: The same issue arises and can be reproduced by using an InputFilter instead of a TextWatcher.

UPDATE: I'd like to clarify that my goal is not to suppress the Suggestions altogether. The issue is that when you prevent specific characters from appearing in the EditText, they still show up in the Suggestions. My goal (which the bounty is for) is to prevent the same specific characters from appearing in the Suggestions.

enter image description here

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北方的巷 2024-12-13 05:50:55

您应该使用 InputFilter 来限制 Edittext< 中的某些字符/代码>

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter}); 

You should use an InputFilter to restrict some characters in Edittext

InputFilter filter = new InputFilter() { 
        public CharSequence filter(CharSequence source, int start, int end, 
Spanned dest, int dstart, int dend) { 
                for (int i = start; i < end; i++) { 
                        if (!Character.isLetterOrDigit(source.charAt(i))) { 
                                return ""; 
                        } 
                } 
                return null; 
        } 
}; 

edit.setFilters(new InputFilter[]{filter}); 
月下凄凉 2024-12-13 05:50:55

模拟器似乎不支持textNoSuggestions和相应的FLAG(TYPE_TEXT_FLAG_NO_SUGGESTIONS)。这确实很烦人,但是嘿:您不是为模拟器用户开发,您不应该专注于此,它在几乎所有设备上都能正常工作。

(请注意,此标志仅适用于 API 级别 5)

It seems that the emulator doesn't support the textNoSuggestions, and the corresponding FLAG (TYPE_TEXT_FLAG_NO_SUGGESTIONS). It's realy anoying, but hey: you're not developing for emulator users, you shouldn't be preoccupied with this, it'll work fine on allmost all devices.

(Note that this flag is available only from API level 5)

不一样的天空 2024-12-13 05:50:55

我们可以在布局 xml 文件中做到这一点,并以简单的方式实现您所要求的内容,在实现这些时插入该行,

android:numeric="your custom elements"
android:digits="your custom elments"
android:inputType="your custom elements"

然后您将能够键入您想要的单词。

We can do that in the layout xml file and achive what you have asked in an easy way, insert the line

android:numeric="your custom elements"
android:digits="your custom elments"
android:inputType="your custom elements"

when you implement these then you will be able to type the words that you want to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文