Analysis of character filtering function of TextView of Android programming _android

Source: Internet
Author: User

This article analyzes the TextView character filtering function of Android programming. Share to everyone for your reference, specific as follows:

TextView can be set to accept a wide variety of characters, by filtering the specified characters to meet the input and display requirements of different applications.

Through XML configuration:

Android:inputtype

Number accepts integer input
numbersigned Accept signed integer input
Numberdecimal accepts integer and decimal input

Android:digits

Specifies that a fixed number, such as android:digits= "012345", is accepted and only the input of 0~5 digits is accepted

Android:numberic

Integers Accept integer input
Signed Accept signed integer input
Decimal accepts integer and decimal input

Setting through Java code

In fact, the above 3 properties, the function is a bit repetitive, and ultimately through the Java code to TextView settings KeyListener

KeyListener is an interface that provides monitoring of input keyboard keys

Inputfilter is an interface that provides filtering on characters

Android provides the numberkeylistener that implements KeyListener and Inputfilter, and Digitskeylistener inherits Numberkeylistener

TextView TV = new TextView (context);
Accept only integer input
keylistener l = new Digitskeylistener (fasle,false);
Accept signed integer input
keylistener l = new Digitskeylistener (true,false);
Accept decimal, Integer input
keylistener l = new Digitskeylistener (false,true);
Accept signed integer/decimal input
keylistener L = new Digitskeylistener (true,true);
Tv.setkeylistener (l);

If you want to achieve a greater degree of freedom of filtering customization, you can write a KeyListener (inherited Basekeylistener) and implement Inputfilter, rewrite the filter () function, in the filter () function can be free filter.

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

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.