带有电子邮件域的 AutoCompleteTextView android [英] AutoCompleteTextView with email domains android

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

问题描述

所以我的应用程序中有一个 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); 

仅此而已.

祝你好运!

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆