开发者

Android: Deleting characters in EditText with TextWatcher still shows characters in suggestions

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.addTe开发者_开发知识库xtChangedListener(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.

Android: Deleting characters in EditText with TextWatcher still shows characters in suggestions


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


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜