{{ v.name }}
{{ v.cls }}类
{{ v.price }} ¥{{ v.price }}
今天,讲讲昨天看到的一个自定义的控件,可以自动补齐邮箱后缀。
效果很好:
由于原有的autocompletetextview只是按照相同的字符串匹配,所以这里要自定义autocompletetextview,然后复写里面的一些方法
publicclassemailautocompletetextviewextendsautocompletetextview{privatestaticfinalstringtag="emailautocompletetextview";privatestring[]emailsufixs=newstring[]{"@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"};publicemailautocompletetextview(contextcontext){super(context);init(context);}publicemailautocompletetextview(contextcontext,attributesetattrs){super(context,attrs);init(context);}publicemailautocompletetextview(contextcontext,attributesetattrs,intdefstyle){super(context,attrs,defstyle);init(context);}publicvoidsetadapterstring(string[]es){if(es!=null&&es.length>0)this.emailsufixs=es;}privatevoidinit(finalcontextcontext){//adapter中使用默认的emailsufixs中的数据,可以通过setadapterstring来更改this.setadapter(newemailautocompleteadapter(context,r.layout.register_auto_complete_item,emailsufixs));//使得在输入1个字符之后便开启自动完成this.setthreshold(1);this.setonfocuschangelistener(newonfocuschangelistener(){@overridepublicvoidonfocuschange(viewv,booleanhasfocus){if(hasfocus){stringtext=emailautocompletetextview.this.gettext().tostring();//当该文本域重新获得焦点后,重启自动完成if(!"".equals(text))performfiltering(text,0);}else{//当文本域丢失焦点后,检查输入email地址的格式emailautocompletetextviewev=(emailautocompletetextview)v;stringtext=ev.gettext().tostring();//这里正则写的有点粗暴:)if(text!=null&&text.matches("^[a-za-z0-9_]+@[a-za-z0-9]+\.[a-za-z0-9]+$")){}else{toasttoast=toast.maketext(context,"邮件地址格式不正确",toast.length_short);toast.show();}}}});}@overrideprotectedvoidreplacetext(charsequencetext){//当我们在下拉框中选择一项时,android会默认使用autocompletetextview中adapter里的文本来填充文本域//因为这里adapter中只是存了常用email的后缀//因此要重新replace逻辑,将用户输入的部分与后缀合并log.i(tag+"replacetext",text.tostring());stringt=this.gettext().tostring();intindex=t.indexof("@");if(index!=-1)t=t.substring(0,index);super.replacetext(t+text);}@overrideprotectedvoidperformfiltering(charsequencetext,intkeycode){//该方法会在用户输入文本之后调用,将已输入的文本与adapter中的数据对比,若它匹配//adapter中数据的前半部分,那么adapter中的这条数据将会在下拉框中出现log.i(tag+"performfiltering",text.tostring()+""+keycode);stringt=text.tostring();//因为用户输入邮箱时,都是以字母,数字开始,而我们的adapter中只会提供以类似于"@163.com"//的邮箱后缀,因此在调用super.performfiltering时,传入的一定是以"@"开头的字符串intindex=t.indexof("@");if(index==-1){if(t.matches("^[a-za-z0-9_]+$")){super.performfiltering("@",keycode);}elsethis.dismissdropdown();//当用户中途输入非法字符时,关闭下拉提示框}else{super.performfiltering(t.substring(index),keycode);}}privateclassemailautocompleteadapterextendsarrayadapter
这里面通过重写autocompletetextview的代码实现邮箱的后缀显示。在初始化时,定义了带邮箱后缀的adapter,当用户输入数据时,把文本框的数据与邮箱后缀拼接的字符串填充到adapter,emailautocompletetextview会自动显示与用户输入的字符串匹配的数据显示在下拉框中。当用户点击时,会自动调用replacetext(charsequencetext)的代码,这里把邮箱的后缀与用户的内容拼接的字符串填充到文本框中。
此外,当用户输入数据时,会自动调用protectedvoidperformfiltering(charsequencetext,intkeycode)的代码,这里由于adapter中只是保存了邮箱的后缀,所以重写此代码,当用户输入邮箱的后缀,如“@163.com”时返回的的时“@”之后的数据,显示匹配的数据。当用户没有输入“@”时,返回“@”这个字符显示所有的邮箱后缀。
现在就可以直接引用了,引用的时候注意自定义文件的路径
源码下载:http://download.csdn.net/detail/bzlj2912009596/9881431
android自定义控件之autocompletetextview邮箱后缀自动补全就讲完了。
就这么简单。