本文主要是介绍Android EditText限制输入字数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//监听输入电话的位数editTel.addTextChangedListener(new TextWatcher() {private CharSequence temp;//监听前的文本 private int editStart;//光标开始位置 private int editEnd;//光标结束位置 private final int charMaxNum = 11; //最大文本数@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {temp = s;}@Overridepublic void afterTextChanged(Editable s) {editStart = editTel.getSelectionStart();editEnd = editTel.getSelectionEnd() ;if (temp.length() > charMaxNum) { Toast.makeText(mApp, "您已经输入了11位电话号码了", 0).show();s.delete(editStart - 1, editEnd);int tempSelection = editStart;editTel.setText(s);editTel.setSelection(tempSelection);}}});
这篇关于Android EditText限制输入字数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!