最近做项目做登录的时候用到了邮箱登录,想实现邮箱的一边输入一边自动填充的功能,上网找了一下,找到了一个基本能够这个功能的例子,网上很多的例子都是在输入“@”之后并不能根据后面输入的内容来过滤掉不符合条件的邮箱,虽然大多数用户在输入完用户名之后就会在下拉列表中找到并点击列表自动填上后面的内容,但总感觉这样不太完美,所以就拿来改了一下,实现了过滤功能。好了,废话不多说了,上代码。
MainActivity.java
AutoTextViewAdapter.java
运行截图:
附上工程源码:
AutoCompleteEmailDemo.zip
MainActivity.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package
com.guet.zhuge;
import
android.app.Activity;
import
android.os.Bundle;
import
android.text.Editable;
import
android.text.TextWatcher;
import
android.widget.AutoCompleteTextView;
public
class
MainActivity
extends
Activity
implements
TextWatcher{
private
AutoCompleteTextView autoview;
private
AutoTextViewAdapter adapter;
private
static
final
String[] AUTO_EMAILS = {
"@163.com"
,
"@sina.com"
,
"@qq.com"
,
"@126.com"
,
"@gmail.com"
,
"@apple.com"
};
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoview = (AutoCompleteTextView) findViewById(R.id.auto_tv);
adapter =
new
AutoTextViewAdapter(
this
);
autoview.setAdapter(adapter);
autoview.setThreshold(
1
);
//输入1个字符时就开始检测,默认为2个
autoview.addTextChangedListener(
this
);
//监听autoview的变化
}
@Override
public
void
afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String input = s.toString();
adapter.mList.clear();
autoAddEmails(input);
adapter.notifyDataSetChanged();
autoview.showDropDown();
}
/**
* 自动填充邮箱列表
* @param input
*/
private
void
autoAddEmails(String input) {
System.out.println(
"input-->"
+ input);
String autoEmail =
""
;
if
(input.length() >
0
) {
for
(
int
i =
0
; i < AUTO_EMAILS.length; ++i) {
if
(input.contains(
"@"
)) {
//包含“@”则开始过滤
String filter = input.substring(input.indexOf(
"@"
) +
1
, input.length());
//获取过滤器,即根据输入“@”之后的内容过滤出符合条件的邮箱
System.out.println(
"filter-->"
+ filter);
if
(AUTO_EMAILS<i>.contains(filter)) {
//符合过滤条件
autoEmail = input.substring(
0
, input.indexOf(
"@"
)) + AUTO_EMAILS<i>;
//用户输入“@”之前的内容加上自动填充的内容即为最后的结果
adapter.mList.add(autoEmail);
}
}
else
{
autoEmail = input + AUTO_EMAILS<i>;
adapter.mList.add(autoEmail);
}
}
}
}
@Override
public
void
beforeTextChanged(CharSequence arg0,
int
arg1,
int
arg2,
int
arg3) {
// TODO Auto-generated method stub
}
@Override
public
void
onTextChanged(CharSequence arg0,
int
arg1,
int
arg2,
int
arg3) {
// TODO Auto-generated method stub
}
}
</i></i></i>
|
AutoTextViewAdapter.java
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package
com.guet.zhuge;
import
java.util.ArrayList;
import
java.util.List;
import
android.content.Context;
import
android.graphics.Color;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.Filter;
import
android.widget.Filterable;
import
android.widget.TextView;
public
class
AutoTextViewAdapter
extends
BaseAdapter
implements
Filterable {
public
List<String> mList;
private
Context mContext;
private
MyFilter mFilter;
public
AutoTextViewAdapter(Context context) {
mContext = context;
mList =
new
ArrayList<String>();
}
@Override
public
int
getCount() {
return
mList ==
null
?
0
: mList.size();
}
@Override
public
Object getItem(
int
position) {
return
mList ==
null
?
null
: mList.get(position);
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
@Override
public
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;
}
public
Filter getFilter() {
if
(mFilter ==
null
) {
mFilter =
new
MyFilter();
}
return
mFilter;
}
private
class
MyFilter
extends
Filter {
@Override
protected
FilterResults performFiltering(CharSequence constraint) {
FilterResults results =
new
FilterResults();
if
(mList ==
null
) {
mList =
new
ArrayList<String>();
}
results.values = mList;
results.count = mList.size();
return
results;
}
@Override
protected
void
publishResults(CharSequence constraint, FilterResults results) {
if
(results.count >
0
) {
notifyDataSetChanged();
}
else
{
notifyDataSetInvalidated();
}
}
}
}
|
运行截图:


附上工程源码:
