實現(xiàn)思路:為EditText設(shè)置監(jiān)聽歌殃,調(diào)用
addTextChangedListener(TextWatcher watcher)
方法。然后在onTextChanged()
方法中處理字符串并顯示到EditText上畸悬,最后我們要在afterTextChanged(Editable s)
方法中將光標(biāo)移動到最后
下面先熟悉一下TextWatcher 接口回調(diào)函數(shù):
public interface TextWatcher extends NoCopySpan {
/**
* This method is called to notify you that, within <code>s</code>,
* the <code>count</code> characters beginning at <code>start</code>
* are about to be replaced by new text with length <code>after</code>.
* It is an error to attempt to make changes to <code>s</code> from
* this callback.
* 在字符串s內(nèi),從索引為start(包含)的字符開始的count個字符將被長度為after的新文
* 本代替
*/
public void beforeTextChanged(CharSequence s, int start,
int count, int after);
/**
* This method is called to notify you that, within <code>s</code>,
* the <code>count</code> characters beginning at <code>start</code>
* have just replaced old text that had length <code>before</code>.
* It is an error to attempt to make changes to <code>s</code> from
* this callback.
* 在字符串s內(nèi),從索引為start(包含)的字符開始count個字符剛剛替換了長度為before的
* 舊字符
*/
public void onTextChanged(CharSequence s, int start, int before, int count);
/**
* This method is called to notify you that, somewhere within
* <code>s</code>, the text has been changed.
* It is legitimate to make further changes to <code>s</code> from
* this callback, but be careful not to get yourself into an infinite
* loop, because any changes you make will cause this method to be
* called again recursively.
* (You are not told where the change took place because other
* afterTextChanged() methods may already have made other changes
* and invalidated the offsets. But if you need to know here,
* you can use {@link Spannable#setSpan} in {@link #onTextChanged}
* to mark your place and then look up from here where the span
* ended up.
* 字符串s內(nèi)容已經(jīng)發(fā)生了變化.可以在這一步對s進行合理的變更,但是要注意不要進入
* 無限循環(huán),因為字符串的任何變化都會再次遞歸調(diào)用此回調(diào)方法.在這個方法中不會告
* 訴 你字符串哪些內(nèi)容發(fā)生了變化,因為其他針對字符串的改變已經(jīng)調(diào)用了
* afterTextChanged().如果你想知道哪些發(fā)生了變化,可以在
* onTextChanged(CharSequence, int, int, int)使用setSpan(Object, int, int, int)做標(biāo)記
*/
public void afterTextChanged(Editable s);
}
實現(xiàn)代碼如下:
注意,手機號碼為11位,但是因為有特殊字符(代碼中將空格替換為想要的字符)或者空格,所以EditText的
android:maxLength="13"
屬性要設(shè)置為大于11究珊,我的由于存在兩個空格,所以設(shè)置最大為13纵苛。
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:digits="1234567890"<!--只允許輸入這10個數(shù)字-->
android:hint="請輸入手機號"
android:inputType="phone|number"<!--設(shè)置鍵盤輸入類型為數(shù)字鍵盤-->
android:maxLength="13" />
etPhone.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int length = s.toString().length();
//刪除數(shù)字
if (count == 0) {
if (length == 4) {
etPhone.setText(s.subSequence(0, 3));
}
if (length == 9) {
etPhone.setText(s.subSequence(0, 8));
}
}
//添加數(shù)字
if (count == 1) {
if (length == 4) {
String part1 = s.subSequence(0, 3).toString();
String part2 = s.subSequence(3, length).toString();
etPhone.setText(part1 + " " + part2);
}
if (length==9){
String part1 = s.subSequence(0, 8).toString();
String part2 = s.subSequence(8, length).toString();
etPhone.setText(part1 + " " + part2);
}
}
}
@Override
public void afterTextChanged(Editable s) {
//將光標(biāo)移動到末尾
etPhone.setSelection(etPhone.getText().toString().length());
//處理s
});
代碼很簡單,下面看看效果圖:
edit_text.png
雖然效果做出來了言津,但是當(dāng)我們調(diào)用etPhone.getText().toString()
方法時攻人,你會發(fā)現(xiàn)得到的結(jié)果里也是包含空格或者特殊字符的一串號碼。如果想要純數(shù)字的結(jié)果悬槽,那么就需要對結(jié)果進行一下處理:
//從EditText獲取的字符串
String phoneNum=etPhone.getText().toString();
//純數(shù)字字符串
String number=formatPhoneNum(phoneNum);
/**
* 去掉手機號內(nèi)除數(shù)字外的所有字符
*
* @param phoneNum 手機號
* @return
*/
private String formatPhoneNum(String phoneNum) {
String regex = "(\\+86)|[^0-9]";
Pattern pattern = Pattern.compile(regex );
Matcher matcher = pattern.matcher(phoneNum);
return matcher.replaceAll("");
}