标签:比较 tab maxlength 条件 bsp append listen final void
众所周知EditView有个inputType 属性可以设置输入的类型。
android:inputType="number"
1 <EditText 2 android:id="@+id/etDigits" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:digits="012345" 6 android:hint="通过digits设置" 7 android:inputType="number" 8 android:maxLength="11" />
1 EditText etTextWatcher = (EditText) findViewById(R.id.etTextWatcher); 2 etTextWatcher.addTextChangedListener(new EditTextWatcher(etTextWatcher)); 3 /** 4 * Created by xugang on 2016/6/23. 5 * 输入监听 6 */ 7 public class EditTextWatcher implements TextWatcher { 8 private EditText editText; 9 public EditTextWatcher(EditText editText) { 10 this.editText = editText; 11 } 12 @Override 13 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 14 } 15 @Override 16 public void onTextChanged(CharSequence s, int start, int before, int count) { 17 if (s != null && s.length() > start) { 18 //输入判断规则 19 if (!RegularUtils.compare(s.toString().trim(), RegularUtils.LIMIT_PHONE_INPUT)) { 20 editText.setText(s.subSequence(0, s.length() - 1)); 21 editText.setSelection(s.length() - 1); 22 } 23 } 24 } 25 @Override 26 public void afterTextChanged(Editable s) { 27 } 28 }
1 public static final String LIMIT_PHONE_INPUT = "^1\\d{0,10}$";//限制只允许输入首位为1的最多11位的数字 2 EditView etInputFilter = (EditView)findViewById(R.id.etInputFilter); 3 etInputFilter.setFilters(new EditInputFilter(LIMIT_PHONE_INPUT)); 4 /** 5 * Created by xugang on 2016/6/22. 6 * EditView过滤器 7 */ 8 public class EditInputFilter implements InputFilter { 9 // 10 private String regular; 11 public EditInputFilter(String regular) { 12 this.regular = regular; 13 } 14 @Override 15 public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { 16 if (regular == null) 17 return source; 18 if (dstart == 0 && dend == 0 && TextUtils.isEmpty(source)) 19 return null; 20 if (dstart == dend) { 21 //输入 22 StringBuilder builder = new StringBuilder(dest); 23 if (builder.append(source).toString().matches(regular)) return source; 24 else return ""; 25 } 26 return source; 27 } 28 }
标签:比较 tab maxlength 条件 bsp append listen final void
原文地址:http://www.cnblogs.com/pullein/p/6232431.html