Android自定义自动完成文本视图

时间:2012-12-13 08:46:56

标签: android autocomplete

我需要自定义AutoCompleteTextView。 我在我的适配器中使用Unicode字符,但问题是用户并不总是拥有或使用带有unicode字符的键盘。

这个想法是当用户开始输入字母C时 我想建议以C,Ć,Č

开头的项目

当用户键入字母S. 我想建议从S和Š

开始的项目

有没有办法让这项工作?

3 个答案:

答案 0 :(得分:0)

您可以实现自己的过滤器,例如不仅返回C *值,还返回C *,Ć*,Č*

如何实现您可以在此处找到的自己的过滤器

Android AutoCompleteTextView with Custom Adapter filtering not working

答案 1 :(得分:0)

您是否尝试过实施标准Android Search界面,但使用FTS3/4表格向ICU tokenizer提出建议?

我还没有实现Unicode搜索,但使用ICU tokenizer可能会有效。也许有人试过它可以评论这个答案吗?

答案 2 :(得分:0)

我已经解决了这个问题:

我编写了这个将unicode字符串转换为非unicode的方法。

/** Remove HTML constants for unicode chars from string */
public static String convertFromUnicode(String text, Context ctx) {

    // Find wrong unicode chars and replace it with non-unicode

    text = text.replaceAll(ch, "c");
    text = text.replaceAll(zh, "z");
    text = text.replaceAll(sh, "s");
    text = text.replaceAll(tj, "c");
    text = text.replaceAll(Ch, "C");
    text = text.replaceAll(Zh, "Z");
    text = text.replaceAll(Sh, "S");
    text = text.replaceAll(Tj, "C");

    text = text.replaceAll(ctx.getResources().getString(R.string.ch), "c");
    text = text.replaceAll(ctx.getResources().getString(R.string.zh), "z");
    text = text.replaceAll(ctx.getResources().getString(R.string.sh), "s");
    text = text.replaceAll(ctx.getResources().getString(R.string.tj), "c");
    text = text.replaceAll(ctx.getResources().getString(R.string.Ch), "C");
    text = text.replaceAll(ctx.getResources().getString(R.string.Zh), "Z");
    text = text.replaceAll(ctx.getResources().getString(R.string.Sh), "S");
    text = text.replaceAll(ctx.getResources().getString(R.string.Tj), "C");

    return text;
}

并在CustomAdaper中的performFiltering中执行:

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null) {
            suggestions.clear();
            for (String showName : itemsAll) {

                String tempshowName = ParserData.convertFromUnicode(showName,
                        getContext());

                String tempContraint = ParserData.convertFromUnicode(
                        constraint.toString(), getContext());

                if (tempshowName.toLowerCase().startsWith(
                        tempContraint.toString().toLowerCase())) {
                    suggestions.add(showName);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
    }

所以unicode而不是unicode字符似乎是平等的。 Tnx for idea @dilix。