【问题标题】:In between of EditText how to use autocomplete android在EditText之间如何使用自动完成android
【发布时间】:2016-01-13 12:32:24
【问题描述】:

我知道如何在 android 中使用 EditText 以及如何在 android 中使用 AutoComplete EditText。但是 AutoComplete EditText 用作下拉列表,其中用户键入字符并显示建议,并且用户单击其中一个建议。如果没有建议,则不会显示任何文本。

我的要求是,考虑有一个 EditText。用户可以输入他们想要的任何内容,但是当单词以特殊字符(例如“@”)开头时,api 将点击并显示建议。

完整示例:

“我的名字是詹姆斯邦德,我是 @And”

在上面的例子中,当用户在“@And”末尾输入这句话时,它会显示点击API的自动建议(应该显示Android),用户可以从列表中选择这个建议的词。

有什么办法可以做到吗?

请帮帮我。

谢谢。

【问题讨论】:

    标签: android autocomplete android-edittext


    【解决方案1】:

    Android 提供MultiAutoCompleteTextView。使用此小部件而不是 EditText 并覆盖其标记器。

    MultiAutoCompleteTextView myautocomplete = (MultiAutoCompleteTextView) findViewById(R.id.multy);
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array_for_autocomplete);
        myautocomplete.setAdapter(adapter);
    
        myautocomplete.setTokenizer(new MultiAutoCompleteTextView.Tokenizer() {
            @Override
            public int findTokenStart(CharSequence text, int cursor) {
                int i = cursor;
    
                while (i > 0 && text.charAt(i - 1) != '@') {
                    i--;
                }
                while (i < cursor && text.charAt(i) == ' ') {
                    i++;
                }
    
                return i;
            }
    
            @Override
            public int findTokenEnd(CharSequence text, int cursor) {
                int i = cursor;
                int len = text.length();
    
                while (i < len) {
                    if (text.charAt(i) == ',') {
                        return i;
                    } else {
                        i++;
                    }
                }
    
                return len;
            }
    
            @Override
            public CharSequence terminateToken(CharSequence text) {
                int i = text.length();
    
                while (i > 0 && text.charAt(i - 1) == ' ') {
                    i--;
                }
    
                if (i > 0 && text.charAt(i - 1) == ',') {
                    return text;
                } else {
                    if (text instanceof Spanned) {
                        SpannableString sp = new SpannableString(text + ", ");
                        TextUtils.copySpansFrom((Spanned) text, 0, text.length(),
                                Object.class, sp, 0);
                        return sp;
                    } else {
                        return text + ", ";
                    }
                }
            }
        });
    

    文档:http://developer.android.com/reference/android/widget/MultiAutoCompleteTextView.html

    【讨论】:

    • AutoCompleteTextView中没有setTokenizer()方法
    • 使用MultiAutoCompleteTextView 并覆盖方法。它确实有 setTokenizer() 方法。
    • 不,这不是我问题的解决方案
    • 我已经更新了我的答案,这次我亲自测试了。它完美地工作。完整的工作代码。
    • 这个其实很好,我可以从建议中选择后删除@吗?应该只显示选定的建议。
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2013-11-02
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多