Android simultaneously controls the number of EditText input characters and the method of prohibiting special character input _android

Source: Internet
Author: User
Tags gettext stringbuffer

This example describes how Android controls both the number of edittext input characters and the prohibition of special character input. Share to everyone for your reference. The specific analysis is as follows:

Here's a summary of three ways to do the following:

Method One:

1. Reference two namespaces:

Import Android.text.TextWatcher; 
Import android.text.Editable;

Used to prevent special character input control

Define EditText Medittext Objects

2. Control character Length:

By Inputfilter filtering to achieve character length control, the advantage is that dynamic length control can be achieved, rather than a length fixed value

int mmaxlenth =;
inputfilter[] Filterarray = new inputfilter[1];
 Filterarray[0] = new Inputfilter () {
  @Override public
  charsequence Filter (charsequence source, int start, int end , 
   spanned dest, int dstart, int dend) {
  Boolean binvlid = false;
  int sourcelen = Getcharacternum (source.tostring ());
  int destlen = Getcharacternum (dest.tostring ());
  if (Sourcelen + destlen > Mmaxlenth) {return
   "";  }
  return source;
  }
 ;
Medittext.setfilters (Filterarray);

3. Prohibit special character input:

 Medittext.addtextchangedlistener (New Textwatcher () {
  String tmp = "";
  String digits = "/\\:*? <>|\" \n\t ";
  @Override public
  void ontextchanged (charsequence s, int start, int before,
   int count) {
  Medittext.setselection (S.length ());
  }
  @Override public
  void beforetextchanged (charsequence s, int start, int count,
   int after) {
  TMP = s.tostring ();
  }
  @Override public
  void aftertextchanged (Editable s) {
  String str = s.tostring ();
  if (Str.equals (TMP)) {return
   ;
  }
  StringBuffer sb = new StringBuffer ();
  for (int i = 0; i < str.length (); i++) {
   if (Digits.indexof (Str.charat (i)) < 0) {
   sb.append (Str.charat (i)) ;
   }
  }
  TMP = Sb.tostring ();
  Medittext.settext (TMP);
  }
 });

Method Two:

1. Add Reference:

Import java.util.regex.*;

2. Define functions:

public static string Stringfilter (String str) throws patternsyntaxexception{ 
 string regEx = "[/\\:*? <>|\" \n\t ]"; The character pattern
 p = pattern.compile (regEx) to be filtered out 
 ; Matcher m = p.matcher (str); 
 Return M.replaceall (""). Trim (); 
 

3. Add Listener Events:

Medittext.addtextchangedlistener (New Textwatcher () {
  @Override public
  void ontextchanged (charsequence s, int Start, int before,
   int count) {
  String t = Medittext.gettext (). toString ();
  String editable = Medittext.gettext (). toString (); 
  String str = stringfilter (editable.tostring ()); 
  if (!editable.equals (str)) { 
   medittext.settext (str); 
   Medittext.setselection (Str.length ()); Cursor after
  } 
  } @Override
  public
  void beforetextchanged (charsequence s, int start, int count,
   int After) {
  }
  @Override public
  void aftertextchanged (Editable s) {
  }
 });

Method Three:

int mmaxlenth = 200;//setting allows character length to be entered public static string Stringfilter (String str) throws patternsyntaxexception{String re
 GEx = "[/\\:*? <>|\" \n\t] ";
 Pattern p = pattern.compile (regEx);
 Matcher m = p.matcher (str);
 Return M.replaceall ("");
  } 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 = stringfilter (editable);
  Filter special characters 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 Publ
   IC void aftertextchanged (Editable s) {if (Cou > Mmaxlenth) {selectionend = Medittext.getselectionend ();
  S.delete (Mmaxlenth, selectionend); }
  }
 });

The above code has been tested, for everyone to share! For the first, two, the two methods in the Samsung note 2 will have a problem (other phones are not a problem), and the input window will appear to bounce, specific reasons, interested friends can debug it!

The third method is fine.

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.