带有电子邮件域的AutoCompleteTextView android

带有电子邮件域的AutoCompleteTextView android,android,autocompletetextview,Android,Autocompletetextview,因此,我的应用程序中有一个autocompletetextview字段,我希望用户输入他的电子邮件地址。现在,为了帮助他更快地键入,并且不出错,我想建议他在键入时使用最常用的电子邮件域服务器 我将该控件用于此数组 String[] arraymails ={"@gmail.com","@hotmail.com","@yahoo.com","@outlook.com"}; 这个在oncreate中 mEmailView = (AutoCompleteTextView) findViewByI

因此,我的应用程序中有一个autocompletetextview字段,我希望用户输入他的电子邮件地址。现在,为了帮助他更快地键入,并且不出错,我想建议他在键入时使用最常用的电子邮件域服务器

我将该控件用于此数组

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);
mEmailView=(AutoCompleteTextView)findViewById(R.id.register\u email);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,ArrayMail);
mEmailView.setAdapter(适配器);
这个想法是,当用户输入“@”字符,然后输入“g”时,它会提示@gmail.com

如果我开始直接在文本框中键入“@g…”,这很好,但是如果我之前键入任何内容,比如john@gm“不行

是否有任何类型的通配符,例如“*@gmail.com”用于执行此操作?或者我应该如何实现它


谢谢

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


几天后,到处寻找解决方案,却找不到。 我发布它是因为它可能会帮助别人

通过反复试验和调查几个网站和指南,我可以找到我正在寻找的解决方案

以下是解决方案外观的图像:

在文本框中,你可以键入任何你想要的内容,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();
        }
    }
};

}
就这些


祝你好运

你能扩展或链接一些例子吗?哦,对不起,我很少打开stackoverflow,现在我的笔记本电脑出现错误,好的,我会在我的笔记本电脑正常后再试,^ ^你可以试试这个,这是一个很好的解决方案。谢谢,很好。只需添加mEmailView.setAdapter(adapter);它节省了我很多时间。非常感谢。
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);