开发者

Use AutoCompleteTextView but hide characters on the keyboard

I'm using AutoCompleteTextView that the user can see the opportunities. So when I tap on two characters e.g. "ba" I will see "Bahamas", "Bahrain","Azerbaijan" etc - this works!

But if I don't have a country start开发者_JAVA技巧ing with the letter "z" I will hide the z on the keyboard. And if I tap the two characters "ba", I will only see "h" on my keyboard. How can I do that? And how can I realize it if I still want to tap "ba" and will get "bahamas" AND "azerbaijan"?

Thanks everyone!


as Cata pointed out, you won't be able to hide the keys on the softkeyboard unless you write your own keyboard - you do have a couple of other options however:

  • you could put a text watcher on the the edit text, and if they key they type isn't in any of the words, you could delete it

  • you could use setKeyListener http://developer.android.com/reference/android/widget/TextView.html#setKeyListener%28android.text.method.KeyListener%29 to and validate their entries.

Both of these will not hide keys from the keyboard but you could use them to prevent the user from typing an invalid key.

As for your second question about matching the user input to the middle of a string, this is not in the code of the autocomplete:

Relevant code form the android source:

for (int i = 0; i < count; i++) {
            final T value = values.get(i);
            final String valueText = value.toString().toLowerCase();

            // First match against the whole, non-splitted value
            if (valueText.startsWith(prefixString)) {
                newValues.add(value);
            } else {
                final String[] words = valueText.split(" ");
                final int wordCount = words.length;

                for (int k = 0; k < wordCount; k++) {
                    if (words[k].startsWith(prefixString)) {
                        newValues.add(value);
                        break;
                    }
                }
            }
        }

you can see it will only match the first characters of any individual word in an item. So to achieve your second goal, you would have to write your own adapter that implements filterable.

here is a tutorial that might get you started on that: http://thinkandroid.wordpress.com/2010/02/08/writing-your-own-autocompletetextview/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜