有个小伙伴遇到了这样一个问题,就是AutoCompleteTextView实现自动填充的功能。同时要具备手机格式化的功能。下拉列表最后一行是有个清除历史的功能。可是点击“清除历史”却把文字要设置进去AutoCompleteTextView中。这样的效果显然很糟糕。所以我就写了这样一个简单的demo。来帮助遇到这种问题的朋友解决这样一个问题。二话不多说直接上代码。
布局文件(activity_main.xml)代码如下:
代码如下 | 复制代码 |
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Please input:"/> android:id="@+id/actv" android:layout_width="fill_parent" android:layout_height="wrap_content"/> |
java文件(MainActivity.java)代码如下:
代码如下 | 复制代码 |
importandroid.app.Activity; importandroid.os.Bundle; importandroid.text.Editable; importandroid.text.Selection; importandroid.text.TextWatcher; importandroid.view.View; importandroid.widget.AdapterView; importandroid.widget.AdapterView.OnItemClickListener; importandroid.widget.ArrayAdapter; importandroid.widget.AutoCompleteTextView; importandroid.widget.EditText; importandroid.widget.TextView; importandroid.widget.Toast; publicclassMainActivityextendsActivity { privateAutoCompleteTextView mAutoCompleteTextView; privateString[] mAutoStrs =newString[] {"138 0013 8000","13800138001", "13800138002","13800138003","13800138004","138 0013 800清除记录"}; privateString mBeforeTextChangedStr =""; publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.actv); ArrayAdapter android.R.layout.simple_dropdown_item_1line, mAutoStrs); mAutoCompleteTextView.setAdapter(_arrayAdapter); mAutoCompleteTextView.setThreshold(1);// 设置输入一个字符就提示 mAutoCompleteTextView.setOnItemClickListener(newOnItemClickListener() { @Override publicvoidonItemClick(AdapterView arg0, View arg1,intarg2, longarg3) { String _clearStr =""; if(arg1instanceofTextView) { _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对象 */ publicvoidphoneNumAddSpaceOne(finalEditText editText) { editText.addTextChangedListener(newTextWatcher() { privateintstart; privateintbefore; privateStringBuilder stringBuilder; @Override publicvoidonTextChanged(CharSequence s,intstart,intbefore, intcount) { this.start = start; this.before = before; } @Override publicvoidbeforeTextChanged(CharSequence s,intstart,intcount, intafter) { } @Override publicvoidafterTextChanged(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 =newStringBuilder(); }else{ stringBuilder.delete(0, stringBuilder.length()); } for(inti =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())) { intindex = 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 */ publicbooleanisNumeric(String str) { for(inti = str.length(); --i >=0;) { intchr = str.charAt(i); if(chr<48|| chr="">57) returnfalse; } returntrue; } } |