电子邮件域android的AutoCompleteTextVie

电子邮件域android的AutoCompleteTextVie

本文介绍了具有电子邮件域android的AutoCompleteTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have an autocompletetextview field in my app which I want the user to enter his email address. Now, to help him type it faster and don't make mistakes, I want to suggest him the most commons email domains servers while typing it.

我将此控件与此数组一起使用

Im using that control with this array

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

,这在oncreate

and this in the 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.

The idea is that, when the user types the "@" character and then "g" it would suggest @gmail.com.

如果我直接在文本框中直接输入"@g ..",则此方法很好,但是如果我之前输入了任何内容,如"john @ gm",它将无法正常工作.

This works fine if i start typing in the textbox directly "@g.." but if i type anything before, like "john@gm" it won't work.

是否存在任何通配符,例如"*@gmail.com"?还是我应该如何实施?

Is there any kind of wildcard character, like a "*@gmail.com" for doing this? or how should i implement it?

谢谢

推荐答案

几天后,到处寻找解决方案,找不到了.我发布它是因为它可能会对某人有所帮助.

After several days and looking everywhere for the solution, couldn't find it.I'm posting it because it might help someone.

通过反复试验并研究几个站点和指南,我可以找到我所寻找的解决方案.

By trial and error and investigating several sites and guides i could find the solution i was looking for.

这是解决方案的图像:

在文本框"中,您可以键入所需的任何内容,约翰就是一个例子.

The Textbox, you can type whatever you want, john is an example.

只需输入"@"即可为您列出所有可能的域

Just typing "@" will give you the list of all possible domains

您可以通过开始输入域名来进行更多过滤

And you could filter a bit more by starting to type the domain name

代码:

CustomFilterAdapter类

CustomFilterAdapter Class

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上(您可以在此处添加任何内容)

On the activity onCreate (you can add here whatever you want)

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);

仅此而已.

祝你好运!

这篇关于具有电子邮件域android的AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:42