First on the code:
Import Java.io.unsupportedencodingexception;import Android.app.activity;import Android.text.editable;import Android.text.textwatcher;import android.util.log;import android.widget.edittext;/** * EditText listener, used to limit the number of words, if the number of words exceeded, is automatically discarded and prompted with a toast * * @author Kyson * */public class Edittextwatcher implements Textwatcher {private static final String TAG = "Edittextwatcher";p rivate charsequence temp;private int selectionstart;private int selectionend;//word limit private int Range = information for 3;//hint private String infostring;private Activity mactivity;private EditText medittext;//Constructor Public EDITTEXTW Atcher (activity activity, EditText text) {super (); this.mactivity = Activity;this.medittext = text;} Set Toast prompt information public void setinfomation (String infostring, int range) {this.range = range;this.infostring = infostring;} public void ontextchanged (charsequence s, int start, int before, int count) {//TODO auto-generated method stubtemp = s;} public void beforetextchanged (charsequence s, int start, int count, int afteR) {//TODO auto-generated method stub}public void aftertextchanged (Editable s) {//for Some reason, we should substract O Neint textLength = 0; try {//we should speciafy the encode->gbktextlength = Temp.tostring (). GetBytes ("GBK"). Length;} C Atch (unsupportedencodingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} if (TextLength > Range) {selectionstart = Medittext.getselectionstart (); selectionend = Medittext.getselectionend () ;//Toast.maketext (EditTextWatcher.this.mActivity, infostring,//toast.length_short). Show (); Toastutils.show (EditTextWatcher.this.mActivity, infostring); S.delete (selectionStart-1, selectionend); int Tempselection = selectionStart-1; LOG.V (TAG, "Input information is:" + S + "infostring" + infostring + "range:" + range); Medittext.settext (s); Medittext.setselection (temp Selection);}}}
The limit of the number of words in Android EditText