android 自定义控件之AutoCompleteTextView邮箱后缀自动补全

发布时间:2024-03-05 点击:108
今天,讲讲昨天看到的一个自定义的控件,可以自动补齐邮箱后缀。
效果很好:
由于原有的autocompletetextview只是按照相同的字符串匹配,所以这里要自定义autocompletetextview,然后复写里面的一些方法
public class emailautocompletetextview extends autocompletetextview { private static final string tag = “emailautocompletetextview”; private string[] emailsufixs = 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”}; public emailautocompletetextview(context context) { super(context); 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); } public void setadapterstring(string[] es) { if (es != null && es.length > 0) this.emailsufixs = es; } private void init(final context context) { //adapter中使用默认的emailsufixs中的数据,可以通过setadapterstring来更改 this.setadapter(new emailautocompleteadapter(context, r.layout.register_auto_complete_item, emailsufixs)); //使得在输入1个字符之后便开启自动完成 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] $”)) { } else { toast toast = toast.maketext(context, “邮件地址格式不正确”, toast.length_short); toast.show(); } } } }); } @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 class emailautocompleteadapter extends arrayadapter<string> { public emailautocompleteadapter(context context, int textviewresourceid, string[] email_s) { super(context, textviewresourceid, email_s); } @override public view getview(int position, view convertview, viewgroup parent) { log.i(tag, “in getview”); view v = convertview; if (v == null) v = layoutinflater.from(getcontext()).inflate( r.layout.register_auto_complete_item, null); textview tv = (textview) v.findviewbyid(r.id.tv); string t = emailautocompletetextview.this.gettext().tostring(); int index = t.indexof(“@”); if (index != -1) t = t.substring(0, index); //将用户输入的文本与adapter中的email后缀拼接后,在下拉框中显示 tv.settext(t getitem(position)); log.i(tag, tv.gettext().tostring()); return v; } } }
这里面通过重写autocompletetextview的代码实现邮箱的后缀显示。在初始化时,定义了带邮箱后缀的adapter,当用户输入数据时,把文本框的数据与邮箱后缀拼接的字符串填充到adapter,emailautocompletetextview会自动显示与用户输入的字符串匹配的数据显示在下拉框中。当用户点击时,会自动调用replacetext(charsequencetext)的代码,这里把邮箱的后缀与用户的内容拼接的字符串填充到文本框中。
此外,当用户输入数据时,会自动调用protectedvoidperformfiltering(charsequencetext,intkeycode)的代码,这里由于adapter中只是保存了邮箱的后缀,所以重写此代码,当用户输入邮箱的后缀,如“@163.com”时返回的的时“@”之后的数据,显示匹配的数据。当用户没有输入“@”时,返回“@”这个字符显示所有的邮箱后缀。
现在就可以直接引用了,引用的时候注意自定义文件的路径
<com.example.view.widgets.emailautocompletetextview android:id=”@ id/act” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:hint=”请输入您常用的邮箱” android:textcolor=”@color/black” android:shadowcolor=”@color/gray” android:shadowradius=”1″ android:numeric=”decimal”/>
源码下载:http://download.csdn.net/detail/bzlj2912009596/9881431
android 自定义控件之autocompletetextview邮箱后缀自动补全 就讲完了。
就这么简单。
如果,你对上面的内容还有疑问,推荐选择西部数码企业云邮箱!有专人协助您解答邮箱疑问。
西部数码企业云邮箱,采用分布式集群架构,数据多份写入,规避单点故障,全球智能中继,更安全稳定。企业邮箱无限空间,极速收发,99.9%精准过滤垃圾邮件,支持层级子文件夹,邮件撤回,日程微信通知、邮件监管、自定义工作报告功能、邮件归档、ssl部署等几十项功能。而且价格实惠,还可以免费试用!
企业邮箱免费试用入口:https://www.west.cn/services/mail/


云服务器会打折吗
ip地址对应多少个域名
云服务器怎么和汽车相连
微信积分商城系统定制提高商家和客户的互动率
在阿里云服务器怎么建网店呢知乎
腾讯云服务器怎么改内存
韩国云服务器怎么选择
国内https证书申请步骤有哪些?https证书免费申请