日韩精品成人无码专区免费-国产99久久久久久免费看-国产精品丝袜久久久久久不卡-国产精品无码一区二区三区

android 自定義控件之AutoCompleteTextView郵箱后綴自動補全

發布時間:2024-03-05 點擊:166
今天,講講昨天看到的一個自定義的控件,可以自動補齊郵箱后綴。
效果很好:
由于原有的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證書免費申請