Sometimes the input box for Android has a limit on the number of characters entered, and displays the number of characters entered. This can be done in the following ways:
1. Child definition Limitnumedittext Inheritance EditText
Import Android.content.context;import Android.content.res.typedarray;import Android.telephony.smsmessage;import Android.text.editable;import Android.text.inputfilter;import Android.text.textwatcher;import Android.util.attributeset;import android.widget.edittext;import us.pinguo.cc.r;/** * Created by crab on 15-3-18. */public class Limitnumedittext extends EditText {private int mmaxlength; Private Ontextcountchangelistener Mtextcountchangelistener; Public Limitnumedittext (Context context) {This (context, NULL); } public Limitnumedittext (context context, AttributeSet Attrs) {Super (context, attrs); TypedArray TypedArray = context.obtainstyledattributes (Attrs, R.styleable.limitnumedittext); Mmaxlength = Typedarray.getint (R.styleable.limitnumedittext_maxlength,-1); Typedarray.recycle (); if (mmaxlength >= 0) {setfilters (new inputfilter[]{new Inputfilter.lengthfilter (Mmaxlength)}); } else {SetFilters(new inputfilter[0]); } addtextchangedlistener (NULL); }/** * @return returns the maximum number of characters for the limit input */public int getlimitlength () {return mmaxlength; } @Override public void Addtextchangedlistener (final Textwatcher watcher) {Textwatcher inner=new textwatcher () {@Override public void beforetextchanged (Charsequence s, int. start, int count, int after) { if (watcher!=null) {watcher.beforetextchanged (s,start,count,after); }} @Override public void ontextchanged (charsequence s, int start, int before, int count) {int[] params= smsmessage.calculatelength (s,false); int use=params[1]; if (mmaxlength>=0 && mtextcountchangelistener!=null) {Mtextcountchangelistener.ontextcountcha Nge (use,mmaxlength); } if (watcher!=null) {WATCHER.ONTEXTCHanged (S,start,before,count); }} @Override public void aftertextchanged (Editable s) {if (watcher!=null) {watcher.aftertextchanged (s); } } }; Super.addtextchangedlistener (inner); } public Limitnumedittext (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); } public void Setontextcountchangelistener (Ontextcountchangelistener listener) {Mtextcountchangelistener=list Ener; }/** * Listen for input box character change */public interface ontextcountchangelistener{/** * * @param use output The size of the word Fuzan * @param total limits the number of inputs to the line */public void Ontextcountchange (int use, int total); }
2. Modify the Res/values/attrs.xml file to add the following line <declare-styleable name= "Limitnumedittext" >
<attr name= "maxLength" format= "integer"/>
</declare-styleable>
3. Using in the layout file
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:algntext= "http// Schemas.android.com/apk/res-auto " android:background=" #FFFFFF " android:orientation=" vertical " Android:layout_width= "Match_parent" android:layout_height= "match_parent" > < Com.example.crab.mycameratest.LimitNumEditText algntext:maxlength= "android:id=" @+id/id_edit_text_ Test " android:layout_width=" match_parent " android:layout_height=" 50DP "/></linearlayout>
Android input Box limit character input number