有个小伙伴遇到了这样一个问题,就是AutoCompleteTextView实现自动填充的功能。同时要具备手机格式化的功能。下拉列表最后一行是有个清除历史的功能。可是点击“清除历史”却把文字要设置进去AutoCompleteTextView中。这样的效果显然很糟糕。所以我就写了这样一个简单的demo。来帮助遇到这种问题的朋友解决这样一个问题。二话不多说直接上代码。
布局文件(activity_main.xml)代码如下:
<&#63;xml version="1.0" encoding="utf-8"&#63;>
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_arrayAdapter = new ArrayAdapter (this, android.R.layout.simple_dropdown_item_1line, mAutoStrs); mAutoCompleteTextView.setAdapter(_arrayAdapter); mAutoCompleteTextView.setThreshold(1);// 设置输入一个字符就提示 mAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<&#63;> 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 = 0;) { int chr = str.charAt(i); if (chr <48 || chr > 57) return false; } return true; } }
以上所述是小编给大家介绍的Android中使用 AutoCompleteTextView 实现手机号格式化附带清空历史的操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!