Android EditText word count detection and limit solution

Android EditText word count detection and limit solution

The control EditText is often used in Android layout, and the content entered in EditText often needs to be limited. We can observe the content entered in the input box through textwatcher.

About coding in Android

Result. Getbytes() is the inverse of new string (byte []).

The front one is string - > byte [], and the back one is byte [] - > string

During Java runtime, there is no difference between string and string. They are stored in memory in the form of 2-byte Unicode. The so-called coding is for converting string into byte []. For example, I can convert "ABC" into a string of data a through UTF-8 or another string of data B through GB2312. This process is string. Getbytes(). For example, "ABC". GetBytes ("UTF-8") gets a and "ABC". GetBytes ("GB2312") gets B. If it is "ABC". GetBytes (), you don't know what code to use, which is platform related.

How to retrieve a string from a string or B string, that is, new string (a, "UTF-8") or new string (B, "GB2312"). Because a is converted from UTF-8, use UTF-8 to return to string. If new string (a, "GB2312"), the Chinese in it is garbled.

The number of bytes of characters in each encoding format is listed below:

English letter: a

Number of bytes: 1; Code: GB2312 bytes: 1; Code: GBK bytes: 1; Code: GB18030 bytes: 1; Code: iso-8859-1 bytes: 1; Code: UTF-8 bytes: 4; Code: utf-16 bytes: 2; Code: utf-16be bytes: 2; Code: utf-16le

Chinese characters: People

Number of bytes: 2; Code: GB2312 bytes: 2; Code: GBK bytes: 2; Code: GB18030 bytes: 1; Code: iso-8859-1 bytes: 3; Code: UTF-8 bytes: 4; Code: utf-16 bytes: 2; Code: utf-16be bytes: 2; Code: utf-16le

According to the above results, we can judge whether it is Chinese or English by the number of UTF-8 bytes of each character.

There is a need to limit the number of characters entered in EditText. There are 15 characters in Chinese and 30 characters in English. Chinese and English can be cross entered, so you can use the above conditions to judge.

The specific implementation is as follows:

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
分享
二维码
< <上一篇
下一篇>>