电子邮件ID建议android

时间:2016-10-06 00:58:13

标签: android autocompletetextview

enter image description here

我希望登录功能与linkedIn app相同,如果用户输入他的电子邮件ID,他应该得到一些共同的建议。如果用户具有类型

'example@' 

然后他应该得到建议清单

example@yahoo.com
example@gmail.com
example@.hotmail.com
example@rediff.com..

只有在输入“@”后才能显示建议。

MainActivity

  autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.text);
  str = autoCompleteTextView.getText().toString();
  indexOfAt = str.indexOf("@");
  ObjectAdapter adapter = new ObjectAdapter(MainActivity.this, searchArrayList);
  autoCompleteTextView.setAdapter(adapter);
  autoCompleteTextView.setThreshold(indexOfAt);

适配器

public class ObjectAdapter extends BaseAdapter implements Filterable {

    private Context context;
    private ArrayList<String> originalList;
    private ArrayList<String> suggestions = new ArrayList<>();
    private Filter filter = new CustomFilter();

    /**
     * @param context      Context
     * @param originalList Original list used to compare in constraints.
     */
    public ObjectAdapter(Context context, ArrayList<String> originalList) {
        this.context = context;
        this.originalList = originalList;
    }

    @Override
    public int getCount() {
        return suggestions.size(); // Return the size of the suggestions list.
    }

    @Override
    public Object getItem(int position) {
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    /**
     * This is where you inflate the layout and also where you set what you want to display.
     * Here we also implement a View Holder in order to recycle the views.
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);

        ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_autotext,
                    parent,
                    false);
            holder = new ViewHolder();
            holder.autoText = (TextView) convertView.findViewById(R.id.autoText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.autoText.setText(suggestions.get(position));

        return convertView;
    }


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

    private static class ViewHolder {
        TextView autoText;
    }

    /**
     * Our Custom Filter Class.
     */
    private class CustomFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            suggestions.clear();

            if (originalList != null && constraint != null) { // Check if the Original List and Constraint aren't null.
                for (int i = 0; i < originalList.size(); i++) {
                    if (originalList.get(i).toLowerCase().contains(constraint)) { // Compare item in original list if it contains constraints.
                        suggestions.add(originalList.get(i)); // If TRUE add item in Suggestions.
                    }
                }
            }
            FilterResults results = new FilterResults(); // Create new Filter Results and return this to publishResults;
            results.values = suggestions;
            results.count = suggestions.size();

            return results;
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须使用AutoCompleteTextView。

<AutoCompleteTextView
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="5dp"
 android:ems="10"
 android:textColor="#FFF"
 android:textColorHint="#999"
 android:textStyle="bold"
 android:padding="10dp"
 android:hint="Email Id"
 android:textSize="20sp"
 android:id="@+id/txtId" >
 <requestFocus />
 </AutoCompleteTextView>

答案 1 :(得分:0)

尝试这个答案

电子邮件编辑文本-自动完成文本视图

   email_edittext.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}

        override fun onTextChanged(s: CharSequence, i: Int, i1: Int, i2: Int) {
            var occurrences = 0
            val enteredString = s.toString()
            for (c in enteredString.toCharArray()) {
                if (c == '@') {
                    occurrences++
                }
            }
            if (occurrences == 1) {
                val requiredString = enteredString.substring(0, enteredString.indexOf("@"))
                val emailList = mutableListOf<String>()
                emailList.add(requiredString + "@gmail.com")
                emailList.add(requiredString + "@hotmail.com")
                emailList.add(requiredString + "@outlook.com")
                emailList.add(requiredString + "@yahoo.com")
                emailList.add(requiredString + "@aol.com")

                val adapter = ArrayAdapter<String>(context!!, android.R.layout.simple_list_item_1, emailList)
                email_edittext.showDropDown()
                email_edittext.setThreshold(0)
                email_edittext.setAdapter(adapter)
            } else if (occurrences == 0) {
                email_edittext.dismissDropDown()
            }
        }

        override fun afterTextChanged(editable: Editable) {}
    })