Set the input condition of EditText in Android, androidedittext

Source: Internet
Author: User

Set the input condition of EditText in Android, androidedittext
I. Application scenarios

In the previous shopping mall applications, user data settings were limited as follows:

(1) only English letters, numbers, and @. can be entered in the user's email address,

(2) a user's mobile phone must be allowed to enter only numbers. Do not enter other characters.

(3) The user password should not contain spaces or Chinese characters.

2. Solutions for users' mailboxes and mobile phones: (1) android: digits attribute in EditText:

It indicates the acceptable character set combination of EditText. By configuring this attribute, only the specified characters can be entered. Here we will list the android: digits configuration in the above three scenarios.

Scenario 1: only numbers android: digits = "0123456789" are allowed"

Scenario 2: only numbers and English letters can be entered. android: digits = "0123456789 abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

Scenario 3: only numbers, English letters, and @. are allowed. android: digits = "0123456789abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ @."

Note the following when using the android: digits attribute:

Android: the digits attribute is hidden and cannot be found directly in the EditText visual attribute list. It can only be configured directly in the XML layout file. After this configuration is added to XML, it can be found in the visual attribute list of EditText.

The character sequence in the android: digits attribute is not affected. For example, you can use android: digits = "0123456789" or android: digits = "9876543210" when only numbers are allowed ".

Android: the digits attribute allows repeated characters, which has no effect on the usage. For example, android: digits = "aaaAAA" and android: digits = "aA" have the same effect.

Android: the digits attribute can only set the characters that can be entered, and cannot set the characters that cannot be entered. That is to say, the android: digits attribute can only be set to a whitelist, and cannot be set to a blacklist.

 

(2) android: inputType attribute in EditText:

Mobile phones with Chinese input methods usually have three types of soft keyboards: Chinese Keyboard, English keyboard, and digital keyboard. You can switch between different types of keyboards. Although the android: digits attribute allows only the specified type of characters to be input in EditText, it does not control the type when the keyboard is popped up. For example, when you set android: digits to allow only numbers, the pop-up keyboard may be an English or Chinese Keyboard. You need to click to switch to a numeric keyboard before entering the keyboard. In android: when digits only allows the input of English and numbers, the keyboard may pop up as a Chinese Keyboard. You also need to click to switch to the English keyboard and number keyboard before entering. If you can directly jump to the corresponding keyboard type when the keyboard is displayed, you do not need to switch.

 

When android: inputType is set to number, the input method is automatically switched to the numeric keyboard when it is enabled. When the input method is enabled, the system automatically switches to the English letter keyboard. Here we will list the android: inputType configuration in the above three scenarios.

Scenario 1: only numbers android: inputType = "number" can be entered ". In fact, when inputType is set to number, only numbers can be entered in EditText, so android: digits = "0123456789" does not need to be set. If android: inputType = "number" and android: digits are both set, the actual characters that can be entered are subject to the android: digits configuration. For example, for android: digits = "01234567 +-", the character that can be entered is "01234567 +-". If android: inputType = "number" is set at the same time ", you can automatically switch to the numeric keyboard.

Scenario 2: enter only numbers and English letters android: inputType = "textVisiblePassword ". The other options seem to be inappropriate. textCapxxx is case-sensitive. textPassword can only be used in the password input box. If you don't mind the several symbols in the soft keyboard, textEmailAddress can also be used here.

Scenario 3: only numbers, English letters, and @. can be entered. android: inputType = "textEmailAddress ".

3. Use Regular Expressions and override TextWatcher to restrict input. The following Code demonstrates how to use this method to set the failure to enter spaces and Chinese characters. Generally, the addTextChangedListener method is used to listen to EditText for operations and processing. this method is also used here. The general idea is to listen to the content entered in EditText, and then clear the content that is not Chinese, that is, leave it blank.
(1) first, I define the TextWatcher class.
Package com. example. panchengjia. limittext; import android. text. editable; import android. text. textWatcher; import android. widget. editText;/*** Created by panhouye on 0007. */public class LimitInputTextWatcher implements TextWatcher {/*** et */private EditText et = null;/*** filter condition */private String regex; /*** default filtering condition (regular: Chinese characters and spaces cannot be entered) * \ u4E00-\ u9FA5 matches Chinese characters \ u0020 matches space */private String DEFAULT_REGEX = "[\ u4E00-\ u9FA5 \ u0020]"; /*** constructor ** @ param et */public LimitInputTextWatcher (EditText et) {this. et = et; this. regex = DEFAULT_REGEX;}/*** constructor ** @ param et * @ param regex filter condition */public LimitInputTextWatcher (EditText et, String regex) {this. et = et; this. regex = regex;} @ Override public void beforeTextChanged (CharSequence charSequence, int I, int i1, int i2) {}@ Override public void onTextChanged (CharSequence charSequence, int I, int i1, int i2) {}@ Override public void afterTextChanged (Editable editable) {String str = editable. toString (); String inputStr = clearLimitStr (regex, str); et. removeTextChangedListener (this); // et. the setText method may cause keyboard changes, so editable is used. replace to display the content editable. replace (0, editable. length (), inputStr. trim (); et. addTextChangedListener (this);}/*** clear non-conforming content ** @ param regex * @ return */private String clearLimitStr (String regex, String str) {return str. replaceAll (regex ,"");}}
(2) implementation code in Activity

 

Package com. example. panchengjia. limittext; import android. support. v7.app. appCompatActivity; import android. OS. bundle; import android. widget. editText; public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); EditText et = (EditText) findViewById (R. id. et); // use the default input rule et in LimitInputTextWatcher. addTextChangedListener (new LimitInputTextWatcher (et); EditText ett = (EditText) findViewById (R. id. ett); // you can only enter Chinese characters. addTextChangedListener (new LimitInputTextWatcher (ett, "[^ \ u4E00-\ u9FA5]");}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.