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.