public class EmailAutoCompleteTextView extends AutoCompleteTextView {
private static final String TAG = "EmailAutoCompleteTextView";
private String[] emailSufixs = new String[] {
"@163.com", "@yahoo.com", "@gail.com", "@126.com"
};
public EmailAutoCompleteTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public EmailAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public EmailAutoCompleteTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
init(context);
}
@Override
protected void replaceText(CharSequence text) {
// 当我们在下拉框中选择一项时,android会默认使用AutoCompleteTextView中Adapter里的文本来填充文本域
// 因为这里Adapter中只是存了常用email的后缀
// 因此要重新replace逻辑,将用户输入的部分与后缀合并
Log.i(TAG + " replaceText", text.toString());
String t = this.getText().toString();
int index = t.indexOf("@");
if (index != -1)
t = t.substring(0, index);
super.replaceText(t + text);
}
@Override
protected void performFiltering(CharSequence text, int keyCode) {
// 该方法会在用户输入文本之后调用,将已输入的文本与adapter中的数据对比,若它匹配
// adapter中数据的前半部分,那么adapter中的这条数据将会在下拉框中出现
Log.i(TAG + " performFiltering", text.toString() + " " + keyCode);
String t = text.toString();
// 因为用户输入邮箱时,都是以字母,数字开始,而我们的adapter中只会提供以类似于"@163.com"
// 的邮箱后缀,因此在调用super.performFiltering时,传入的一定是以"@"开头的字符串
int index = t.indexOf("@");
if (index == -1) {
if (t.matches("^[a-zA-Z0-9_]+$")) {
super.performFiltering("@", keyCode);
}
else
this.dismissDropDown();// 当用户中途输入非法字符时,关闭下拉提示框
} else {
super.performFiltering(t.substring(index), keyCode);
}
}
private void init(final Context context) {
// 设置数据集
setAdapter(new EmailAutoCompleteAdapter(context, R.layout.email_list_item, emailSufixs));
this.setThreshold(1);
this.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String text = EmailAutoCompleteTextView.this.getText().toString();
// 当该文本域重新获得焦点后,重启自动完成
if (!"".equals(text))
performFiltering(text, 0);
} else {
// 当文本域丢失焦点后,检查输入email地址的格式
EmailAutoCompleteTextView ev = (EmailAutoCompleteTextView) v;
String text = ev.getText().toString();
// 这里正则写的有点粗暴:)
if (text != null && text.matches("^[a-zA-Z0-9_]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+$")) {
Toast to = new Toast(context);
ImageView i = new ImageView(context);
i.setBackgroundResource(R.drawable.ic_launcher);
to.setView(i);
to.show();
} else {
Toast toast = Toast.makeText(context, "邮件地址格式不正确", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 50);
toast.show();
}
}
}
});
}
private class EmailAutoCompleteAdapter extends ArrayAdapter<String> {
public EmailAutoCompleteAdapter(Context context, int resource,
String[] objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
View v = convertView;
if (v == null)
v = LayoutInflater.from(getContext()).inflate(R.layout.email_list_item, null);
TextView text = (TextView) v.findViewById(R.id.text);
String str = EmailAutoCompleteTextView.this.getText().toString();
int index = str.indexOf("@");
// 如果没有写到@
if (index != -1)
str = str.substring(0, index);
text.setText(str + getItem(position));
return v;
}
}
list_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />