AutoCompleteTextView实现邮箱自动填充
来源:互联网 发布:51黑单片机论坛 编辑:程序博客网 时间:2024/05/22 07:50
最近做项目做登录的时候用到了邮箱登录,想实现邮箱的一边输入一边自动填充的功能,上网找了一下,找到了一个基本能够这个功能的例子,网上很多的例子都是在输入“@”之后并不能根据后面输入的内容来过滤掉不符合条件的邮箱,虽然大多数用户在输入完用户名之后就会在下拉列表中找到并点击列表自动填上后面的内容,但总感觉这样不太完美,所以就拿来改了一下,实现了过滤功能。好了,废话不多说了,上代码。
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();
}
}
}
}
运行截图:
附上工程源码: AutoCompleteEmailDemo.zip
- AutoCompleteTextView实现邮箱自动填充
- AutoCompleteTextView自动填充邮箱后缀
- AutoCompleteTextView实现邮件地址自动填充
- AutoCompleteTextView 自动填充文本框
- 仿网易音乐登陆 android 邮箱自动填充 基于AutoCompleteTextView 修改
- 输入内容自动填充: AutoCompleteTextView
- 自动填充邮箱后缀
- 邮箱自动填充js
- TextWatcher+AutoCompleteTextView实现邮箱过滤器
- android邮箱自动填充功能
- 邮箱下拉自动填充选择
- AutoCompleteTextView实现自动提示
- AutoCompleteTextView实现自动提示
- AutoCompleteTextView实现自动提示
- AutoCompleteTextView实现自动提示
- 邮箱收件人效果(自动填充)
- 类似邮箱自动填充下拉效果
- AutoCompleteTextView+SQLite实现自动检索
- C++ 学习之构造函数:构造函数小结3 默认构造函数、隐式类型转换和复制构造函数
- ubuntu12下安装的php5.3
- HashSet,TreeSet和LinkedHashSet的区别
- python 打开网页方法
- JS的数据类型:number,string,boolean,null,undefined,object,function
- AutoCompleteTextView实现邮箱自动填充
- Graphical_Layout 不显示自定义控件 解决方法
- HP-unix支持大于2G的文件的配置方法
- 怎样让 dma_alloc_coherent() 可以申请更大的内存
- Android ViewPager使用详解
- spice Application.cpp ---function_to_func_name 2013-5-28
- 如何下载和离线安装Chrome的CRX扩展文件包
- 面试最讨厌被问到的问题
- ffmpeg+RTP+H.264 总结