Android Code settings edittext only enter numbers, letters

Source: Internet
Author: User



How to set edittext so that only numbers or certain letters can be entered?

First, set the EditText, enter only the number:

Method 1: Generate the Digitskeylistener object directly.

Et_1.setkeylistener (New
Digitskeylistener (false,true));

Method 2: Set the property in EditText, android:numeric= "integer" to enter only integers, as follows

Android:singleline= "true"

Android:numeric= "Integer"

/>

Method 3: Create a new char[] and add the characters that are allowed to be entered. As follows

Edittext.setkeylistener (New
Numberkeylistener () {

Protected char[] Getacceptedchars ()

{

Char[]
numberchars[]={' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 ',};

return numberchars;

}

});

Second, set EditText can only enter certain letters, such as the following settings Edtitext can only enter a-n,a-n these letters. Here's how:

Edittext.setkeylistener (New
Numberkeylistener () {

Protected char[] Getacceptedchars ()

{

Char[]
Numberchars[]={' A, ' B ', ' C ', ' d ', ' e ', ' f ', ' a ', ' B ', ' C ', ' d '};

return numberchars;

}

});

EditText Et;et = (EditText) Findviewbyid (r.id.et);//Method 1: Establish a digitskeylistener, Then set it to your edittext's keylistenerdigitskeylistener Numericonlylistener = new
Digitskeylistener (false,true); Et.setkeylistener (numericonlylistener);//
Method 2: Set a numberkeylistener for EditText, and then override the Getacceptedchars () method and the Getinputtype () method Et.setkeylistener (new Numberkeylistener () {@Overrideprotected char[] Getacceptedchars () {return new char[] {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' 0 '
};} @Overridepublic int Getinputtype () {//TODO auto-generated method Stubreturn Android.text.InputType.TYPE_CLASS_PHONE;}});

--------------------------------------------------------------------------------------------

01.EditText et;02.et = (EditText) Findviewbyid (r.id.et); 03.//Method 1: Establish a digitskeylistener, Then set it to your edittext's keylistener04.digitskeylistener Numericonlylistener = new
Digitskeylistener (false,true); 05.et.setkeylistener (Numericonlylistener); 06.//
Method 2: Set a numberkeylistener for EditText, and then override the Getacceptedchars () method and the Getinputtype () method 07.et.setkeylistener (new Numberkeylistener () {@Override09. Protected char[] Getacceptedchars () {Ten. return new char[] {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ' , ' 6 ', ' 7 ', ' 8 ', ' 9 ',
' 0 '};11. }12. @Override13. public int Getinputtype () {.//TODO auto-generated method stub15. Return android.text.inputtype.type_class_phone;16.} 17.});

Summary:

The first type can be a decimal number.

The second is that only integers can be entered because of the type_class_phone set. and more flexible.

============================================

Many netizens may find that edittext sometimes need to restrict user input when developing Android, but we can usually use regular expressions to restrict the content, but Android
The EditText input type has been prepared for us, so the singular has the following advantages:

1. The development is simpler, the execution speed is efficient. 2.
The input method will be changed by default, for example, when set to numeric, the input method will automatically display only the number, not the alphabet in the QWERTY.

Below we pass the layout of EditText
XML file to implement the relevant properties:

1. Password Box Property android:password= "true"
This allows EditText to be displayed automatically as an asterisk, and the content will become * in 1 seconds.

2. Pure Digital android:numeric= "true"
This allows the input method to automatically become a digital input keyboard, while allowing only 0-9 of the digital input

3. Only allow android:capitalize= "cwj1987"
This allows only input cwj1987 to be accepted, typically for password authentication

Here are some of the extended style attributes

Android:editable= "false"
Set EditText not editable

Android:singleline= "true"
Force input on a single line

Android:ellipsize= "End"
Auto-hide trailing overflow data, typically used for long lines of text that cannot be displayed in full.

Related Article

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.