How to Use the EditText control in Android Studio

Source: Internet
Author: User

How to Use the EditText control in Android Studio
1. Create a HelloEditText Project

The creation settings are as follows:

Project name: HelloEditText Build Target: android 2.2 Application name: HelloEditText Package name: com. flysnow create Activity: HelloEditText min SDK 8

At this time, the EditText cannot be seen during running, because we have not added it. modify main. xml as follows:

Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent">


Ii. EditText

EditText is a very important component. It can be said that it is a data transmission window between users and Android applications. With EditText, it is equivalent to a door for transmitting data with Android applications, you can pass the data to the Android app and get the desired data.

EditText is a subclass of TextView, so the methods and features of TextView also exist in EditText. For details about TextView, refer to Chapter 6 of the Android series in the previous section: use of TextView widgets-with hyperlinks and running lights

3. Length and blank prompt text, prompt text color, editable or not

EditText has some attributes that can be used to set EditText features, such as the maximum length and blank prompt text.

Sometimes we have some special requirements. We only need to enter a specific number of characters in EditText, such as ID card numbers and mobile phone numbers. In this case, you can use the android: maxLength attribute to set the maximum number of input characters. For example, android: maxLength = "4" indicates that up to four characters can be entered, if there is more, the input will not be entered. Blank text. Sometimes we need to explain what the EditText you defined is for, for example, to input a "User Name" or a "phone number, but you don't want to add a TextView in front of EditText to indicate that this is the input "User Name", because this will use a TextView. What should you do? EditText provides android: hint to set the text displayed when the EditText content is empty. This text is only displayed when the EditText is empty and disappears when you enter the characters, it does not affect the text of your EditText .. Modify main. xml as follows:
Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> "Br =" ">
The color has changed .. Another useful function is to set EditText to uneditable. Setting android: enabled = "false" can enable uneditable and focus. At this time, we can see that EditText is similar to a TextView:
Implement text fields similar to Textarea in html. There is no specialized text domain component in Android, but the same text domain function can be achieved by setting the EditText height. Modify main. xml as follows:
Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent">
4. Enter characters in special format

When developing a program, we can enter characters with unique characters, such as passwords (the characters in the input box must be encrypted), phone numbers (such as numbers and-), and numbers, these are characters in special format. The powerful EditText also provides us with settings for entering these special format characters.

Password text box. Password Input is also a common function in Android apps. You can configure EditText's android: password = "true" to implement this password input function. modify main. xml as follows:
Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent">
We can see that the character we entered has been replaced by a mask like. SMS calls are essential on mobile phones. Therefore, text boxes used to enter phone numbers are also useful, it is much easier to verify the phone number (because the characters are correct, as long as the verification format ). by setting android: phoneNumber = "true", you can change EditText to a text box that only accepts phone number input. Even the keyboard has become a dial-up dedicated keyboard, so you don't have to worry about entering other characters. Modify main. xml as follows:
Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent">
Note that the soft keyboard has become a dial-up dedicated one. sometimes we only want to enter numbers and letters. EditText provides android: numeric to control the input number types. There are three types of numbers: integer (positive integer) and signed (signed integer) and decimal (floating point number ). Take the signed type as an example. modify main. xml as follows:
Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent">
Note that the keyboard changes to "numeric keyboard". 5. Specify a specific soft keyboard type for the text.

Previously, we specified a specific phone number format, and then the keyboard type was changed to a dedicated dial-up keyboard, which is automatically changed. In fact, we can also set the text type through android: inputType, select an appropriate soft keyboard for the input method .. Android: inputType has many types. Here we use the date type for demonstration. modify main. xml as follows:

Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent">

 

Vi. settings of the Enter key icon

The Enter key of the soft keyboard displays the "complete" text by default. We know that pressing Enter indicates that the preparations are complete and what to go. For example, in a search, we Enter the text to be searched, and press Enter to search. However, the default Enter key displays the "finished" text, which is not suitable, it does not conform to the search syntax. If the word "Search" is displayed or a search icon is displayed, how nice is it. Facts have proved that our ideas are reasonable, and Android also provides us with such features. Set android: imeOptions to change the default "finish" text. Here are several common constant values:

ActionUnspecified is not specified, corresponding to constant EditorInfo. IME_ACTION_UNSPECIFIED. effect: actionNone has no action, corresponding to the constant EditorInfo. IME_ACTION_NONE: actionGo, which corresponds to the constant EditorInfo. IME_ACTION_GO: actionSearch, which corresponds to the constant EditorInfo. IME_ACTION_SEARCH effect: Send by actionSend, corresponding to the constant EditorInfo. IME_ACTION_SEND: Next To actionNext, corresponding to the constant EditorInfo. IME_ACTION_NEXT: actionDone completed, corresponding to the constant EditorInfo. IME_ACTION_DONE effect:

The following search is used as an example to demonstrate how to modify main. xml:

Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> Java code packagecom. flysnow; importandroid. app. activity; importandroid. OS. bundle; importandroid. view. keyEvent; importandroid. widget. editText; importandroid. widget. textView; importandroid. widget. toast; importandroid. widget. textView. oneditexceptionlistener; publicclassHelloEditTextextendsActivity {/** Calledwhentheactivityisfirstcreated. * // @ Override publicvoidonCreate (BundlesavedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); EditTexteditText = (EditText) findViewById (R. id. edit_text); editText. setoneditexceptionlistener (newoneditexceptionlistener () {@ Override publicbooleanoneditexception (TextViewv, intactionId, KeyEventevent) {Toast. makeText (HelloEditText. this, String. valueOf (actionId), Toast. LENGTH_SHORT ). show (); returnfalse ;}});}}

Run the program and click Enter (that is, the soft keyboard button of the search icon) to display the actionId. Each of the above settings corresponds to a constant. The actionId here is the constant value.

7. EditText value, full selection, partial selection, and retrieval of selected text

The following example shows how to modify the value of EditText, select all, partially select, and obtain the selected text. main. xml as follows:

Xml Code Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> Java code packagecom. flysnow; importandroid. app. activity; importandroid. OS. bundle; importandroid. text. editable; importandroid. text. selection; importandroid. view. keyEvent; importandroid. view. view; importandroid. view. view. onClickListener; importandroid. widget. button; importandroid. widget. editText; importandroid. widget. textView; importandroid. widget. toast; importandroid. widget. textView. oneditexceptionlistener;/*** EditText demo * @ author feixue heartless * @ since2010-11-29 */publicclassHelloEditTextextendsActivity {/** Calledwhentheactivityisfirstcreated. * // @ Override publicvoidonCreate (BundlesavedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); finalEditTexteditText = (EditText) findViewById (R. id. edit_text); // listen to the Enter key editText. setoneditexceptionlistener (newoneditexceptionlistener () {@ Override publicbooleanoneditexception (TextViewv, intactionId, KeyEventevent) {Toast. makeText (HelloEditText. this, String. valueOf (actionId), Toast. LENGTH_SHORT ). show (); returnfalse ;}}); // obtain the EditText ButtongetValue = (Button) findViewById (R. id. btn_get_value); getValue. setOnClickListener (newOnClickListener () {@ Override publicvoidonClick (Viewv) {Toast. makeText (HelloEditText. this, editText. getText (). toString (), Toast. LENGTH_SHORT ). show () ;}}); // select Buttonall = (Button) findViewById (R. id. btn_all); all. setOnClickListener (newOnClickListener () {@ Override publicvoidonClick (Viewv) {editText. selectAll () ;}}); // select the EditText text Buttonselect = (Button) findViewById (R. id. btn_select); select. setOnClickListener (newOnClickListener () {@ Override publicvoidonClick (Viewv) {Editableeditable = editText. getText (); Selection. setSelection (editable, 1, editable. length () ;}}); // obtain the selected text ButtongetSelect = (Button) findViewById (R. id. btn_get_select); getSelect. setOnClickListener (newOnClickListener () {@ Override publicvoidonClick (Viewv) {intstart = editText. getSelectionStart (); intend = editText. getSelectionEnd (); CharSequenceselectText = editText. getText (). subSequence (start, end); Toast. makeText (HelloEditText. this, selectText, Toast. LENGTH_SHORT ). show () ;}}) ;}/ *** exchange two indexes * @ paramstart start Index * @ paramend end Index */protectedvoidswitchIndex (intstart, intend) {inttemp = start; start = end; end = temp ;}}

The running effect is as follows:



You can enter the text and click the button below to test.

8. Summary

This section details most of the features and common functions of EditText, such as frequently-used password boxes and getting values. I have not updated these busy days. This update is a long one. It can be digested for a while.

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.