Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of automatically adding spaces every 4 bits in EditText by Android

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Android how to achieve EditText every 4 automatically add spaces effect", the content is easy to understand, clear, hope to help you solve the doubt, the following let the editor lead you to study and learn "Android how to achieve EditText every 4 automatically add spaces" this article.

The ideas are as follows:

When the content of the input box changes, the content is split into a character, adding a space in the middle of every 4 bits, and the last 4 bits cannot be added. The purpose of this method of concatenating characters is to solve the problem that when the user deletes the middle number, it will lead to the dislocation of spaces. When the user deletes the middle character, record the action and record the cursor position to ensure that the cursor position is where it should be after the reordering is complete.

In about these two steps, this function can be achieved. In the next step, we first add spaces to ensure that the content always satisfies the space after 4 digits:

Let's take a look at the monitoring of EditText:

Et_credit_number.addTextChangedListener (new TextWatcher () {@ Override public void beforeTextChanged (CharSequence s, int start, int count, int after) {} @ Override public void onTextChanged (CharSequence s, int start, int before, int count) {} @ Override public void afterTextChanged (Editable s) {/ / get the contents of the input box, String etContent = EditTextUtils.getText (et_credit_number) cannot be left blank; if (TextUtils.isEmpty (etContent)) {bt_submit.setEnabled (false); return } / / re-concatenate the string String newContent = AppUtils.addSpeaceByCredit (etContent); / / if changed, repopulate / / prevent EditText infinite setText () from generating an endless loop if (! etContent.equals (newContent)) {et_credit_number.setText (newContent) / / make sure the cursor is at the end, because each setText will cause the cursor to reset / / which basically solves the problem of et_credit_number.setSelection (newContent.length ());} / / determines whether the credit card format is satisfied, and pay attention to the blanks to judge if (MatcheUtils.isCreditNumber (newContent.replaceAll (",")) {bt_submit.setEnabled (true); return;} bt_submit.setEnabled (false);}})

There is no difficulty. I encapsulated the re-spliced string separately:

Public static String addSpeaceByCredit (String content) {if (TextUtils.isEmpty (content)) {return ";} / blank content = content.replaceAll (","); if (TextUtils.isEmpty (content)) {return ";} / / the card number is limited to 16-bit if (content.length () > 16) {content = content.substring (0,16);} StringBuilder newString = new StringBuilder (); for (int I = 1; I 0) {return } String editTextContent = EditTextUtils.getText (et_credit_number); if (TextUtils.isEmpty (editTextContent) | | TextUtils.isEmpty (lastString)) {return;} editTextContent = AppUtils.addSpeaceByCredit (editTextContent); / / if the latest length is less than the last length, if (editTextContent.length () newContent.length ()? NewContent.length (): deleteSelect);} / / determine whether the credit card format is satisfied, and check if (MatcheUtils.isCreditNumber (newContent.replaceAll (",")) {bt_submit.setEnabled (true); return;} bt_submit.setEnabled (false);}})

Here, we mainly use the monitoring of onTextChanged () to determine that the user action is a delete operation, and save the location of the cursor.

The above is all the content of the article "how Android can automatically add spaces every 4 digits in EditText". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report