// Set the character filtering function (to filter out unnecessary characters) public static string stringfilter (string Str) throws patternsyntaxexception {string RegEx = "[/\\:*? <> | \ "\ N \ t]"; pattern P = pattern. compile (RegEx); matcher M = P. matcher (STR); Return M. replaceall ("");} int mmaxlenth = 50; medittext. addtextchangedlistener (New textwatcher () {private int cou = 0; int selectionend = 0; @ override public void ontextchanged (charsequence S, int start, int before, int count) {cou = before + count; string editable = medittext. gettext (). tostring (); string STR = stringfilt Er (editable); If (! Editable. equals (STR) {medittext. settext (STR);} medittext. setselection (medittext. length (); cou = medittext. length () ;}@ override public void beforetextchanged (charsequence S, int start, int count, int after) {}@ override public void aftertextchanged (editable S) {If (COU> mmaxlenth) {selectionend = medittext. getselectionend (); S. delete (mmaxlenth, selectionend); If (androidversion. charat (0)> = '4') {medittext. settext (S. tostring ());}}}});
reference: http://software.intel.com/zh-cn/blogs/2013/03/07/android-edittext? Utm_campaign = csdn & utm_source = intel.csdn.net & utm_medium = link & utm_content = Android % E2 % 80% 20 edittext