您好,登錄后才能下訂單哦!
有個(gè)小伙伴遇到了這樣一個(gè)問題,就是AutoCompleteTextView實(shí)現(xiàn)自動(dòng)填充的功能。同時(shí)要具備手機(jī)格式化的功能。下拉列表最后一行是有個(gè)清除歷史的功能??墒屈c(diǎn)擊“清除歷史”卻把文字要設(shè)置進(jìn)去AutoCompleteTextView中。這樣的效果顯然很糟糕。所以我就寫了這樣一個(gè)簡(jiǎn)單的demo。來(lái)幫助遇到這種問題的朋友解決這樣一個(gè)問題。二話不多說直接上代碼。
布局文件(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);// 設(shè)置輸入一個(gè)字符就提示 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); } /** * 手機(jī)號(hào)格式化代碼 * * @param editText * EditText對(duì)象 */ 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; // 手機(jī)號(hào)格式化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); } } }); } /** * 判斷字符串是否是數(shù)字 * * @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 實(shí)現(xiàn)手機(jī)號(hào)格式化附帶清空歷史的操作,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。