Properties of EditText in Android

Source: Internet
Author: User

This is a reprint of others, very good

1 , edittext the input text is the password form setting

(1) Set in. xml:

Set the EditText to: android:password= "true"//with "." Form display text

(2) Set in code:

Implement a hidden password or this display password by setting EditText's Settransformationmethod () method.

Edittext.settransformationmethod (Passwordtransformationmethod.getinstance ());//Setting the password is not visible.

2 , (1) edittext input text is the phone number

Android:phonenumber= "true"//Enter phone number

3 , EditText word limit settings

(1) Set in. xml: Android:maxlength= "50"

(2) settings in the code:

Edittext.setfilters (New Inputfilter[]{newinputfilter.lengthfilter (100)});

4. EditText Setting font

Android:typeface= "monospace"//Set font. Glyphs are: normal, sans, serif,monospace

5 , whether EditText can be edited

Android:editable//Is editable

6, the soft keyboard in the EditText, the switch off

(1) EditText has focus (focusable is true) prevents Input method pop-up

edittext= (EditText) Findviewbyid (r.id.txtbody);

Edittext.setontouchlistener (New Ontouchlistener () {

public boolean OnTouch (View V, motionevent event) {

Edittext.setinputtype (Inputtype.type_null); Turn off the soft keyboard

return false;

}

});

(2) block Input Method popup when Eidttext (Focusable=false) is not in focus

Inputmethodmanager IMM =

(Inputmethodmanager) Getsystemservice (Input_method_service);

Imm.hidesoftinputfromwindow (Edittext.getwindowtoken (), 0);

(3) Call the numeric keypad and set the input type and keyboard to English

Etnumber.setinputtype (Inputtype.type_class_number); Calling the numeric keypad

Rledittext.setinputtype (inputtype.type_text_flag_multi_line);//Set input type and keyboard to English or: android:inputtype= "textUri| Textmultiline "

(4) android:focusable= "false"//keyboard never pops up

<activity android:name= ". Addlinkman "android:windowsoftinputmode=" Adjustunspecified|statehidden "/>//does not automatically eject the keyboard

Inputmethodmanager IMM = (inputmethodmanager) getsystemservice (Context.input_method_service) by turning off the keyboard (for example, after the input is finished); Imm.hidesoftinputfromwindow (Etedittext.getwindowtoken (), 0);

Auto Eject keyboard

((Inputmethodmanager) Getsystemservice (Input_method_service)). Togglesoftinput (0,inputmethodmanager.hide_not_ Always);

Etedittext.requestfocus ();//Let EditText get focus, but get focus does not automatically eject the keyboard

7. The difference between android:layout_gravity and android:gravity

(1) android:layout_gravity is the gravity direction of this element to the parent element.

(2) android:gravity is the gravity direction of all child elements of this element.

8 , android:padding and Android:layout_margin differences

Both of these can set margins, but there are subtle differences:

(1) android:padding is the margin relative to the parent view

(2) Android:layout_margin is relative to the same level view margin

Example: LinearLayout is a horizontal layout with two buttons below,

(a) If the button on the right side wants to be 15px from the left button, because these two buttons are the same level, should use Android:layout_margin;

(b) If the right button wants to be 350px from the left, it should be used android:padding

9, android:numeric//only accept numbers

Android:numeric to control the number type of the input, a total of three kinds are integer (positive integer), signed (signed integer, positive or negative), and decimal (floating point).

10. Settings for the ENTER key icon

The ENTER key of the soft keyboard displays the "Done" text by default, and we know that pressing ENTER indicates that the predecessor work is ready and what to go for. For example, in a search, we enter the text to search for, and then press ENTER to go to search, but the default enter to display the "done" text, looking not very suitable, does not conform to the semantics of the search, if you can display "search" two words or display a search icon is much better. It turns out that our ideas are reasonable and Android also provides us with such features. Change the default "done" text by setting android:imeoptions. Here are some common constant values:

(1) actionunspecified unspecified, corresponding constant editorinfo.ime_action_unspecified effect:

(2) Actionnone no action, corresponding to the constant Editorinfo.ime_action_none effect:

(3) Actiongo, corresponding to the constant Editorinfo.ime_action_go effect:

(4) Actionsearch search, corresponding to the constant Editorinfo.ime_action_search effect:

(5) Actionsend send, corresponding constant Editorinfo.ime_action_send effect:

(6) Actionnext Next, the corresponding constant Editorinfo.ime_action_next effect:

(7) Actiondone complete, corresponding to the constant Editorinfo.ime_action_done effect:

11. Use Android:imeoptinos to make some interface settings on the Android-powered soft keyboard:

android:imeoptions= "Flagnoextractui"//Make the soft keyboard not full-screen, only occupy a portion of the screen at the same time, this property can also control the soft keyboard lower right button display content, by default, enter the key android:imeoptions= " Actionnone "//input box to the right without any hint android:imeoptions=" Actiongo "//The lower right corner of the key content is ' start ' android:imeoptions= ' actionsearch '// Lower right button for magnifying glass image, search android:imeoptions= "actionsend"//Bottom right button content for ' send ' android:imeoptions= ' actionnext '///lower right corner key content for ' next ' android:imeoptions= "Actiondone"//Bottom right button content is ' done '

12, limit edittext can enter numbers and letters, and the default input is a number, such as ID card number

Android:inputtype= "number" android:digits= "0123456789xyzXYZ"

13, the adjustment of the soft keyboard causes the original interface to be squeezed up, or cause the tab navigation below the interface is squeezed up, the solution is as follows

Workaround:

Use the Android:windowsoftinputmode "Adjustpan" property of activity in manifest.

Also note: For problems with soft keyboards, refer to the Android:windowsoftinputmode properties.

14, EditText cursor detailed edittext.requestfocusfromtouch ();//Let the cursor into the click position. Edittext.requestfocus ();//The default way to get focus
EditText editor = (EditText) getcurrentview ();//the cursor is inserted int cursor = Editor.getselectionstart (); Editor.gettext (). Insert (Cursor,delta);

Let the cursor move to the end (so the text will appear forward) EditText et = ... String Text = "text"; Et.settext (text); Et.setselection (Text.length ());

Android:cursorvisible= "false" to hide the cursor

Android:background= "#00000000"//Don't text box background

Android-Input Method control and default focus settings for text boxes
In the development, the essential will use to the text box (EditText) to carry on the data entry, also will need to have some control to the input method.
First look at the attributes in the layout definition file that are related to the input method:
Property name
Description
Android:inputtype
Specifies the type of input method, int type, which can be selected with | multiple. The value can be referenced by: Android.text.InputType class. Values include: text,  Texturi, Phone,number, etc.
Android:imeoptions
Specify the function of Enter key in IME window, the optional value is normal,  actionnext,actiondone,actionsearch and so on. Some input methods may not be good enough to support this.
The layout definition file below shows some examples of the use of InputType and imeoptions.
<edittextandroid:id= "@+id/textnormal"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Android:hint= "Normaltext"
Android:inputtype= "Text"
Android:imeoptions= "Actionnext"
/>
<edittextandroid:id= "@+id/textinteger"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Android:hint= "Integeronly"
Android:inputtype= "Number"
Android:imeoptions= "Actionnext"
/>
<edittextandroid:id= "@+id/textphone"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Android:hint= "PhoneNumber"
Android:inputtype= "Phone"
Android:imeoptions= "Actionnext"
/>
<edittextandroid:id= "@+id/textemail"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Android:hint= "Email"
Android:imeoptions= "Actionsend"
Android:inputtype= "Textemailaddress"
/>
<edittextandroid:id= "@+id/textsite"
Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content"
Android:hint= "WebSite"
Android:imeoptions= "Actiondone"
Android:inputtype= "Texturi"
/> sometimes also have to set the default focus of intent, not in the intent jump when the default focus (cursor) on the EditText, leading to enter the intent open input method, affecting the interface beautiful.
The order of the default focus is the first control that can be entered from the top down from left to right as the focus
You can use:
Button.setfocusable (TRUE);
Button.requestfocus ();
Button.setfocusableintouchmode (TRUE);
You can also:
Put an invisible linearlayout in front of EditText and let him take the lead: <linearlayoutandroid:focusable= "true" android:focusableintouchmode= "True" android:layout_width= "0px" android:layout_height= "0px"/>
Android EditText Properties Summary

Android EditText Properties Summary

android:layout_gravity= "center_vertical" Settings control display location: Default top, here is centered, and bottom android:hint= "Please enter a number!" "

Set the information displayed on the Space android:numeric= "integer" setting can only enter an integer, if it is a decimal:

Decimal android:singleline= "True" to set the single-line input, and once set to True, the text is not wrapped automatically.

Android:password= "true" setting can only enter password

Android:textcolor = "#ff8c00" Font Color

android:textstyle= "bold" font, bold, italic, Bolditalic android:textsize= "20dip" size

Android:capitalize = "Characters" is written in uppercase letters

android:textalign= "center" EditText does not have this attribute, but TextView has android:textcolorhighlight= "#cccccc" is the background of the selected text, the default is blue

Android:textcolorhint= "#ffff00" sets the color of the hint text, the default is gray android:textscalex= "1.5" controls the spacing between words and words

Android:typeface= "monospace" font, normal, sans, serif, monospace android:background= "@null" space background, not here, refers to transparent

android:layout_weight= "1" weights, which control the position between controls, are useful when controlling the size of the control display.

Android:textappearance= "? Android:attr/textappearancelargeinverse" text appearance, here refers to the system comes with a look,? Indicates whether the system has this appearance, otherwise the default appearance is used. I don't know if I understand that, right? Implemented by edittext the relevant properties in the layout XML file:

1. The Password Box property android:password= "True" allows the EditText to be displayed automatically as an asterisk, and the content will be changed to * within 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. Allow only android:capitalize= "cwj1987" This allows only input cwj1987 to be accepted, generally for password authentication here are some extended style attributes

Android:editable= "false" setting EditText not editable

Android:singleline= "true" forces the input to be in a single line

Android:ellipsize= "End" automatically hides trailing overflow data, typically used for long lines of text that cannot be displayed all

Properties of EditText in Android

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.