Method 1: Medittext. setfilters (New inputfilter [] {New inputfilter. lengthfilter (constants. max_text_input_length )}); Method 2: although this method is complex, I prefer this method. Private textwatcher mtextwatcher = new textwatcher (){ Toast mtoast = NULL; Public void beforetextchanged (charsequence S, int start, Int count, int after ){ } Public void ontextchanged (charsequence S, int start, Int before, int count ){ }
Public void aftertextchanged (editable s ){ Int nselstart = 0; Int nselend = 0; Boolean novermaxlength = false;
Nselstart = medittext. getselectionstart (); Nselend = medittext. getselectionend ();
Novermaxlength = (S. Length ()> constants. max_text_input_length )? True: false; If (novermaxlength ){ If (null = mtoast ){ Mtoast = toast. maketext (mcontext, R. String. ids_msg_text_over_maxlength, Toast. length_short ); } Mtoast. Show ();
S. Delete (nselstart-1, nselend ); Medittext. settextkeepstate (s); // please pay attention to this line to keep the cursor at its original position, while medittext. settext (s) will let the cursor go to the front, // Even adding medittext. setselection (nselstart) does not work. } } }; Android edittext input limit:
Method 1: // Limit the number of words in the input box Edittext. addtextchangedlistener (New textwatcher (){ Private charsequence temp; Private Boolean isedit = true; Private int selectionstart; Private int selectionend; @ Override Public void beforetextchanged (charsequence S, int arg1, int arg2, Int arg3 ){ Temp = s; }
@ Override Public void ontextchanged (charsequence S, int arg1, int arg2, Int arg3 ){ }
@ Override Public void aftertextchanged (editable s ){ Selectionstart = edittext. getselectionstart (); Selectionend = edittext. getselectionend (); Log. I ("gongbiao1", "" + selectionstart ); If (temp. Length ()> constant. text_max ){ Toast. maketext (kaguhomeactivity. This, R. String. edit_content_limit, Toast. length_short) . Show (); S. Delete (selectionStart-1, selectionend ); Int tempselection = selectionstart; Edittext. settext (s ); Edittext. setselection (tempselection ); } }
});
Method 2: Edittext can be used to set the filter feature and customize a lengthfilter. When the number of words entered exceeds the limit, a custom prompt is displayed. // Limit the number of words in the input box Inputfilter [] Filters = new inputfilter [1]; Filters [0] = new inputfilter. lengthfilter (constant. text_max ){ @ Override Public charsequence filter (charsequence source, int start, int end, Spanned DEST, int dstart, int dend ){ If (source. Length ()> 0 & DeST. Length () = constant. text_max ){ If (system. currenttimemillis ()-toasttime)> interval ){ Toasttime = system. currenttimemillis (); Toast . Maketext (kaguhomeactivity. This, R. String. edit_content_limit, Toast. length_short). Show (); } } If (DEST. tostring (). Equals ( Getresources (). getstring (R. String. input_default_txt ))){ Bundle DATA = new bundle (); Data. putcharsequence ("Source", source ); Message message = texthandler. obtainmessage (); Message. setdata (data ); Message. sendtotarget (); }
Return super. Filter (source, start, end, DEST, dstart, dend ); } }; Edittext. setfilters (filters ); Private handler texthandler = new handler (){ @ Override Public void handlemessage (Message MSG ){
Bundle DATA = msg. getdata (); Charsequence source = data. getcharsequence ("Source "); Edittext. settextcolor (color. Black ); Edittext. settext (source ); Edittext. setselection (source. Length ()); } }; |
|
|