This article analyzes the character filtering function of TextView in Android programming. Share it for your reference, as follows:
TextView can be set to accept various characters, and filter the specified characters to meet the input and display requirements of different applications.
Configure via xml:
android:InputType
Accept integer input
numberSigned Accept signed integer input
numberDecimal Accept inputs to integers and decimals
android:digits
Specify to accept fixed numbers, such as android:digits="012345", only inputs from 0 to 5 numbers are accepted.
android:numberic
integer Accept integer input
signed Accept signed integer input
decimal Accept inputs of integers and decimals
Settings via java code
In fact, the functions of the above three properties are a bit duplicated, and in the end, they all set KeyListener for TextView through Java code.
KeyListener is an interface that provides monitoring of input keyboard keys
InputFilter is an interface that provides filtering characters
Android provides NumberKeyListener that implements KeyListener and InputFilter, while DigitsKeyListener inherits NumberKeyListener
TextView tv = new TextView(context); //Only accept integer inputKeyListener l = new DigitsKeyListener(fasle,false); //Accept signed integer inputKeyListener l = new DigitsKeyListener(true,false); //Accept decimals and integer inputKeyListener l = new DigitsKeyListener(false,true); //Accept signed integers/decimal inputKeyListener l = new DigitsKeyListener(true,true); (l);
If you want to achieve greater freedom of filtering customization, you can write a KeyListener (inheriting BaseKeyListener) and implement InputFilter, rewrite the filter() function, and implement free filtering in the filter() function.
I hope this article will be helpful to everyone's Android programming design.