Android中使用 AutoCompleteTextView 实现手机号格式化附带清空历史的操作
程序员文章站
2023-12-10 12:01:58
有个小伙伴遇到了这样一个问题,就是autocompletetextview实现自动填充的功能。同时要具备手机格式化的功能。下拉列表最后一行是有个清除历史的功能。可是点击“清...
有个小伙伴遇到了这样一个问题,就是autocompletetextview实现自动填充的功能。同时要具备手机格式化的功能。下拉列表最后一行是有个清除历史的功能。可是点击“清除历史”却把文字要设置进去autocompletetextview中。这样的效果显然很糟糕。所以我就写了这样一个简单的demo。来帮助遇到这种问题的朋友解决这样一个问题。二话不多说直接上代码。
布局文件(activity_main.xml)代码如下:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="please input:" /> <autocompletetextview android:id="@+id/actv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </linearlayout>
java文件(mainactivity.java)代码如下:
import android.app.activity; import android.os.bundle; import android.text.editable; import android.text.selection; import android.text.textwatcher; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { private autocompletetextview mautocompletetextview; private string[] mautostrs = new string[] { "138 0013 8000", "13800138001", "13800138002", "13800138003", "13800138004", "138 0013 800清除记录" }; private string mbeforetextchangedstr = ""; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mautocompletetextview = (autocompletetextview) findviewbyid(r.id.actv); arrayadapter<string> _arrayadapter = new arrayadapter<string>(this, android.r.layout.simple_dropdown_item_1line, mautostrs); mautocompletetextview.setadapter(_arrayadapter); mautocompletetextview.setthreshold(1);// 设置输入一个字符就提示 mautocompletetextview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> arg0, view arg1, int arg2, long arg3) { string _clearstr = ""; if (arg1 instanceof textview) { _clearstr = ((textview) arg1).gettext().tostring(); } if (_clearstr.equals("138 0013 800清楚记录")) { mautocompletetextview.settext(mbeforetextchangedstr); editable _editable = mautocompletetextview.gettext(); selection.setselection(_editable, _editable.length()); toast.maketext(mainactivity.this, "清除成功了!", toast.length_long).show(); } } }); phonenumaddspaceone(mautocompletetextview); } /** * 手机号格式化代码 * * @param edittext * edittext对象 */ public void phonenumaddspaceone(final edittext edittext) { edittext.addtextchangedlistener(new textwatcher() { private int start; private int before; private stringbuilder stringbuilder; @override public void ontextchanged(charsequence s, int start, int before, int count) { this.start = start; this.before = before; } @override public void beforetextchanged(charsequence s, int start, int count, int after) { } @override public void aftertextchanged(editable s) { string _str = s.tostring(); if (!isnumeric(_str.replace(" ", ""))) { return; } mbeforetextchangedstr = _str; // 手机号格式化xxx xxxx xxxx if (s == null || s.length() == 0) return; if (stringbuilder == null) { stringbuilder = new stringbuilder(); } else { stringbuilder.delete(0, stringbuilder.length()); } for (int i = 0; i < s.length(); i++) { if (i != 3 && i != 8 && s.charat(i) == ' ') { continue; } else { stringbuilder.append(s.charat(i)); if ((stringbuilder.length() == 4 || stringbuilder .length() == 9)&& stringbuilder.charat(stringbuilder.length() - 1) != ' ') { stringbuilder.insert(stringbuilder.length() - 1,' '); } } } if (!stringbuilder.tostring().equals(s.tostring())) { int index = start + 1; if (stringbuilder.charat(start) == ' ') { if (before == 0) { index++; } else { index--; } } else { if (before == 1) { index--; } } edittext.settext(stringbuilder.tostring()); edittext.setselection(index); } } }); } /** * 判断字符串是否是数字 * * @param str * 需要判断的字符串 * @return */ public boolean isnumeric(string str) { for (int i = str.length(); --i >= 0;) { int chr = str.charat(i); if (chr < 48 || chr > 57) return false; } return true; } }
以上所述是小编给大家介绍的android中使用 autocompletetextview 实现手机号格式化附带清空历史的操作,希望对大家有所帮助