1 InputFilter filter = new InputFilter() { 2 3 @Override 4 public CharSequence filter(CharSequence source, int start, int end, 5 Spanned dest, int dstart, int dend) { 6 // TODO Auto-generated method stub 7 for (int i = start; i < end; i++) 8 { 9 //在这里判断输入的只能是英文字母和符号'-'10 if (!Character.isLetter(source.charAt(i))11 && source.charAt(i) != '-') 12 {13 return "";14 }15 }16 return null;17 }18 };19 20 etActivteCode.setFilters(new InputFilter[] { filter });21 etActivteCode.addTextChangedListener(new TextWatcher() {22 23 @Override24 public void onTextChanged(CharSequence s, int start, int before,25 int count) {26 // TODO Auto-generated method stub27 28 etActivteCode.removeTextChangedListener(this);// 解除文字改变事件29 30 if ((s.length() == 4 || s.length() == 9 || s.length() == 14)) {31 etActivteCode.setText(s.toString().toUpperCase() + "-");// 转换32 } else {33 etActivteCode.setText(s.toString().toUpperCase());// 转换34 }35 36 etActivteCode.setSelection(s.toString().length());// 重新设置光标位置37 etActivteCode.addTextChangedListener(this);// 重新绑38 String licensePlateNumber = etActivteCode.getText().toString()39 .trim();40 }41 42 @Override43 public void beforeTextChanged(CharSequence s, int start, int count,44 int after) {45 // TODO Auto-generated method stub46 }47 48 @Override49 public void afterTextChanged(Editable s) {50 // TODO Auto-generated method stub51 52 if (s.length() == 19) {53 buttonActivate.setVisibility(View.VISIBLE);54 InputMethodUtils.HideKeyboard(etActivteCode);55 }56 57 }58 });59 etActivteCode.setOnKeyListener(new OnKeyListener() {60 @Override61 public boolean onKey(View v, int keyCode, KeyEvent event) {62 // TODO Auto-generated method stub63 if (keyCode == KeyEvent.KEYCODE_DEL) {64 CommonUtils.LogWuwei(tag, "delete");65 etActivteCode.setText("");66 buttonActivate.setVisibility(View.INVISIBLE);67 }68 return false;69 }70 });71 }
效果如下: