布局文件(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;
}
}
下载说明
☉本站所有源码和资源均由站长亲自测试-绝对保证都可以架设,运营!
☉如源码和资源有损坏或所有链接均不能下载,请告知管理员,
☉本站软件和源码大部分为站长独资,资源购买和收集,放心下载!
☉唯一站长QQ:1004003180 [人格担保-本站注重诚信!]
☉购买建议E-mail:1004003180@qq.com 源码收购 E-mail:1004003180@qq.com
☉本站文件解压密码 【文章内都自带解压密码,每个密码不同!】