Method One: Using Textwatcher
Java code
- 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 Two: Using Inputfilter
Java code
- Edittext.setfilters (new inputfilter[]{new Inputfilter.lengthfilter (100)}); //100 maximum input words
Method Three: Set in XML
XML code
- <EditText
- .
- .
- .
- android:maxlength="
- />
Android---3 ways to limit the number of editview input words (reprint)