Summary of Android EditText methods for limiting input character types

Summary of Android EditText methods for limiting input character types

preface:

In recent projects, we need to limit the type of EditText input characters, so we sorted out the methods that can realize this function:

1. The first method is implemented through the inputtype of EditText, which can be set through XML or java files. If I want to set it to display the password, I can set it as follows:

In XML

In the java file, you can use myedittext.setinputtype (inputtype. Type_text_variation_password); Of course, there are more other properties for input settings.

2. The second is set through the Android: digits attribute. This method can indicate the characters to be displayed. For example, I want to limit the display of only numbers, which can be as follows:

If there are many contents to be displayed, it will be more troublesome. Write the contents to be displayed in turn.

3. Judge by regular expression. The following example allows only letters, numbers and Chinese characters to be displayed.

Then you need to call this function in onTextChanged () of TextWatcher.

4. Implemented through inputfilter.

To implement the inputfilter filter, you need to override a method called filter.

The following implementation makes EditText only receive characters (numbers, letters and Chinese characters) and "-" and "", and character.isletterordigit will also treat Chinese as letter.

In addition, using inputfilter can also limit the number of input characters, such as

The above code can limit the maximum number of characters to 10.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>