Inputfilter implement EditText text input filter Instance code parsing _android

Source: Internet
Author: User

EditText is an Android text-entry box control that people on earth should all know, but the default edittext is not input, sometimes we need to design a software, and it is for children to use, and need to screen the following minors can not be involved in content, What do you do when you block some text that can't be typed?

In fact, Android SDK has been for us to think about this problem, it provides us with a thing called inputfilter, through which we can easily design a filter to screen some of the user's input, or to change the input to some benign content, so as to solve the above problems.

Implement Inputfilter filter, need to cover a method called filter.

Public abstract charsequence Filter ( 
charsequence source,//input text 
int start,//start position 
int end,//ending position 
spanned dest,//current displayed content 
int dstart,//current start position 
int dend//current end position 
);

The following implementation inputfilter is automatically replaced with "ataaw.com" regardless of what is entered.

Edittext.setfilters (new inputfilter[] { 
new Inputfilter () {Public 
charsequence filter (charsequence src, int Start, int end, spanned DST, int dstart, int dend) {return 
src.length () < 1? dst.subsequence (DStart, DEnd): "at Aaw.com "; 
} 

From the above example we can implement such a function, so that the EditText control can not enter any content, how to do? Only need to replace the above "ataaw.com" to "" can be, each input is equal to not enter any content.

Here's a few two APIs that I'm not using, easy to forget:

Use of IndexOf ():

Returns the first occurrence of string string in the parent string in the indexof character (string).

For example: String ss = "1111.";

int index = ss. IndexOf (".");

The value of index is 4;//starting from 0

SUBSTRING usage:

str=str.substring (int beginindex); intercepts the string of STR from the first letter length to Beginindex, assigning the remaining string to STR;
str=str.substring (int beginindex,int endindex); intercepts the string from the beginning of the beginindex to the end of the Endindex in STR and assigns it to STR;

Like what:

String S1 = "1234567890ABCDEFGH";
S1 = s1.substring (10);

The value of S1 is: ABCDEFGH

String S1 = "1234567890ABCDEFGH";
S1 = s1.substring (0,9);
System.out.println (S1);

The value of S1 is: 123456789

The above is a small series to introduce the Inputfilter implementation of EditText text input filter Instance code analysis, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.