您好,登錄后才能下訂單哦!
手機號 3-4-4控制
package com.gx_school.util; import com.gx_school.activity.ArriveActivity; import android.content.Context; import android.text.Editable; import android.text.TextWatcher; import android.util.Log; import android.widget.EditText; public class EditTextWatcher{ private Context mContext; public EditTextWatcher(Context context){ mContext = context; } /** * 限制輸入小數點后兩位 * @param editText */ public void setPricePoint(final EditText editText) { editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { String str = s.toString(); if(str.length() > 13){ str = str.substring(0, 13); Log.v("result", str); editText.setText(str); editText.setSelection(13); return; } if((str.length() == 3 || str.length() == 8) && str.charAt(str.length() - 1) != '-' && before < count){ str += "-"; editText.setText(str); editText.setSelection(str.length()); return; } //刪除時去掉橫線 if((str.length() == 3 || str.length() == 8) && str.charAt(str.length() - 1) != '-' && before > count){ str = str.substring(0, str.length() - 1); editText.setText(str); editText.setSelection(str.length()); return; } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.v("result", "beforeTextChanged " + s.toString()); } @Override public void afterTextChanged(Editable s) { Log.v("result", "afterTextChanged " + s.toString()); } }); } /** * 限制輸入的長度 * @param editText * @param len */ public void setEditLength(final EditText editText, final int len) { editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s.toString().length() > len){ String str = s.toString().subSequence(0, len).toString(); editText.setText(str); editText.setSelection(str.length()); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); } }
引用
phoneNum = (EditText) findViewById(R.id.et_arriver_phoneNumber);
new EditTextWatcher(ArriveActivity.this).setPricePoint(phoneNum);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。