布局
<LinearLayoutandroid:orientation="vertical"android:layout_marginTop="50dp"android:layout_width="match_parent"android:layout_height="wrap_content"><AutoCompleteTextViewandroid:id="@+id/autoComplete"android:layout_width="match_parent"android:layout_height="66dp" /><test.com.mp3.MailBoxAssociateViewandroid:id="@+id/imageviewedittextone"android:layout_width="match_parent"android:layout_height="58dp"android:layout_marginBottom="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"android:hint="地址"android:paddingLeft="5dp"android:popupBackground="@android:color/darker_gray"android:singleLine="true"android:textColor="@android:color/black"android:textColorHint="@android:color/holo_blue_bright"android:textSize="16sp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/autoComplete" /></LinearLayout>
自定义 MailBoxAssociateView
package test.com.mp3;import android.content.Context;
import android.util.AttributeSet;/*** Created by T_baby on 17/12/25.* @后进行提示*/public class MailBoxAssociateView extends android.support.v7.widget.AppCompatMultiAutoCompleteTextView {public MailBoxAssociateView(Context context) {super(context);}public MailBoxAssociateView(Context context, AttributeSet attrs) {super(context, attrs);}public MailBoxAssociateView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}@Overridepublic boolean enoughToFilter() {// 如果字符中包含'@'并且不在第一位,则满足条件return getText().toString().contains("@") && getText().toString().indexOf("@") > 0;}
}
类 MailBoxAssociate tokenizer
package test.com.mp3;import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.widget.MultiAutoCompleteTextView;/*** Created by T_baby on 17/12/25.*/public class MailBoxAssociateTokenizer implements MultiAutoCompleteTextView.Tokenizer {@Overridepublic int findTokenEnd(CharSequence text, int cursor) {int i = cursor;int len = text.length();while (i < len) {if (text.charAt(i) == '@') {return i;} else {i++;}}return len;}@Overridepublic int findTokenStart(CharSequence text, int cursor) {int index = text.toString().indexOf("@");if (index < 0) {index = text.length();}if (index >= findTokenEnd(text, cursor)) {index = 0;}return index;}@Overridepublic CharSequence terminateToken(CharSequence text) {int i = text.length();while (i > 0 && text.charAt(i - 1) == ' ') {i--;}if (i > 0 && text.charAt(i - 1) == '@') {return text;} else {if (text instanceof Spanned) {SpannableString sp = new SpannableString(text);TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, sp, 0);return sp;} else {return text;}}}}
类 Autocompale
package test.com.mp3;import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;/*** Created by T_baby on 17/12/25.* 自动提示*/public class AutoComplete extends AutoCompleteTextView {//输入字符进行联想,@163.com、@126.com、@qq.com、@sina.com、@yeah.net、@yahoo.com、@gmail.com等常用邮箱private static final String[] emailSuffix = {"@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com","@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com", "@vip.sina.com"};public AutoComplete(Context context) {super(context);init(context);}public AutoComplete(Context context, AttributeSet attrs) {super(context, attrs);init(context);}private void init(Context context) {final MyAdatper adapter = new MyAdatper(context);setAdapter(adapter);addTextChangedListener(new TextWatcher() {@Overridepublic void afterTextChanged(Editable s) {String input = s.toString();adapter.mList.clear();if (input.length() > 0) {for (int i = 0; i < emailSuffix.length; ++i) {adapter.mList.add(input + emailSuffix[i]);}}adapter.notifyDataSetChanged();showDropDown();}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}});// default=2 当输入一个字符的时候就开始检测setThreshold(1);}class MyAdatper extends BaseAdapter implements Filterable {List<String> mList;private Context mContext;private MyFilter mFilter;public MyAdatper(Context context) {mContext = context;mList = new ArrayList<String>();}@Overridepublic int getCount() {return mList == null ? 0 : mList.size();}@Overridepublic Object getItem(int position) {return mList == null ? null : mList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if (convertView == null) {TextView tv = new TextView(mContext);tv.setTextColor(Color.BLACK);tv.setTextSize(20);convertView = tv;}TextView txt = (TextView) convertView;txt.setText(mList.get(position));return txt;}@Overridepublic Filter getFilter() {if (mFilter == null) {mFilter = new MyFilter();}return mFilter;}private class MyFilter extends Filter {@Overrideprotected FilterResults performFiltering(CharSequence constraint) {FilterResults results = new FilterResults();if (mList == null) {mList = new ArrayList<String>();}results.values = mList;results.count = mList.size();return results;}@Overrideprotected void publishResults(CharSequence constraint, FilterResults results) {if (results.count > 0) {notifyDataSetChanged();} else {notifyDataSetInvalidated();}}}}
}
MainActivity
MailBoxAssociateView view;
private String[] email_sufixs = new String[]{"@qq.com", "@163.com", "@126.com", "@gmail.com", "@sina.com", "@hotmail.com","@yahoo.cn", "@sohu.com", "@foxmail.com", "@139.com", "@yeah.net", "@vip.qq.com", "@vip.sina.com"};
在onCreateview中
view = findViewById(R.id.imageviewedittextone); MailBoxAssociateTokenizer mailBoxAssociateTokenizer = new MailBoxAssociateTokenizer(); ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, email_sufixs); view.setAdapter(adapter); view.setTokenizer(mailBoxAssociateTokenizer);