自动完成文本框与电子邮件域名 Android

5

我在我的应用程序中拥有一个自动完成文本视图字段,我希望用户输入他的电子邮件地址。为了帮助他更快地输入并避免错误,我想在输入时向他建议最常见的电子邮件域名服务器。

我正在使用这个数组来控制它:

String[] arraymails ={"@gmail.com","@hotmail.com","@yahoo.com","@outlook.com"};  

在onCreate中添加以下内容。
mEmailView = (AutoCompleteTextView) findViewById(R.id.register_email);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arraymails);  
mEmailView.setAdapter(adapter);

这个想法是,当用户键入“@”字符然后是“g”时,它会建议使用 @gmail.com。
如果我直接在文本框中开始输入“@g..”,那么这个功能很好用。但如果我先输入一些文字,例如“john@gm”,那么它就不起作用了。
有没有任何通配符字符,比如“* @gmail.com”可以实现此功能?还是我应该如何实现它?
谢谢。
2个回答

20
几天来四处寻找解决方案,但却找不到。我发布这篇文章是因为它可能会帮助别人。通过多次尝试和调查多个网站和指南,我找到了我正在寻找的解决方案。以下是解决方案的图片:文本框,您可以键入任何内容,例如John。只需键入“@”即可获取所有可能的域列表,并且您可以通过开始输入域名来进行更多筛选。代码:CustomFilterAdapter类。
public class CustomFilterAdapter extends ArrayAdapter<String> {
    private final String MY_DEBUG_TAG = "CustomFilterAdapter";
    private ArrayList<String> items;
    private ArrayList<String> itemsAll;
    private ArrayList<String> suggestions;
    private int viewResourceId;

public CustomFilterAdapter(Context context, int viewResourceId, ArrayList<String> items) {
    super(context, viewResourceId, items);
    this.items = items;
    this.itemsAll = (ArrayList<String>) items.clone();
    this.suggestions = new ArrayList<String>();
    this.viewResourceId = viewResourceId;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(viewResourceId, null);
    }
    String customer = items.get(position);
    if (customer != null) {
        TextView customerNameLabel = (TextView)v;
        if (customerNameLabel != null) {
            customerNameLabel.setText(customer);
        }
    }
    return v;
}

@Override
public Filter getFilter() {
    return nameFilter;
}

Filter nameFilter = new Filter() {
    public String convertResultToString(Object resultValue) {
        String str = (String)resultValue; 
        return str;
    }
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        if (constraint != null){
        String palabra = constraint.toString();
        if(palabra != null && palabra.indexOf("@") != -1) {
            String palabra2 = palabra.substring(palabra.indexOf("@"));
            String antesArroba;
            try{
                antesArroba = palabra.substring(0, palabra.indexOf("@"));
            }catch (Exception ex)
            {
                antesArroba ="";
            }
            suggestions.clear();
            for (String customer : itemsAll) {
                if(customer.toLowerCase().startsWith(palabra2.toString().toLowerCase())){
                    suggestions.add(antesArroba+customer);
                }
            }
            FilterResults filterResults = new FilterResults();
            filterResults.values = suggestions;
            filterResults.count = suggestions.size();
            return filterResults;
        } else {
            return new FilterResults();
        }
        }else {
            return new FilterResults();
        }
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        ArrayList<String> filteredList = (ArrayList<String>) results.values;
        if(results != null && results.count > 0) {
            clear();
            for (String c : filteredList) {
                add(c);
            }
            notifyDataSetChanged();
        }
    }
};

}

在活动的onCreate方法中(您可以在此处添加任何内容)

arraymails = new ArrayList();
arraymails.add("@gmail.com");
arraymails.add("@hotmail.com");
arraymails.add("@yahoo.com");
arraymails.add("@outlook.com");
arraymails.add("@adinet.com.uy");

mEmailView = (AutoCompleteTextView) findViewById(R.id.register_email);
mEmailView.setText(mEmail);
CustomFilterAdapter adapter = new CustomFilterAdapter(this,android.R.layout.simple_list_item_1,arraymails); 

就是这样了。

祝你好运!


1
这是一个很棒的解决方案。谢谢。 - Lev
3
好的。你只需要添加mEmailView.setAdapter(adapter);即可。 - Elad
1
它为我节省了很多时间。谢谢。 - Vikalp

0
我认为你必须通过扩展BaseAdapter来创建自己的适配器,在getView方法中你必须在那里创建自己的逻辑。

示例链接


1
你能否提供一些更详细的例子或链接? - klifa
哦,对不起,我很少打开StackOverflow,现在我的笔记本电脑出现了错误,好的,等我的电脑没问题后我会尝试的,^_^ - Fahriyal Afif
你可以尝试这个:https://dev59.com/_Woy5IYBdhLWcg3wFqFT - Fahriyal Afif

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接