Go Android Learning: How to use EditText

Source: Internet
Author: User

EditText is a control that is often used in Android development and is used primarily to obtain input from users.  1.edittext Common Properties  edittext inherit from TextView, so EditText also has some properties and methods in TextView. The common properties of EditText are as follows:  android:capitalize= "sentences"//Set the English letter capital type, sentences only the first letter uppercase; words each word first letter size, with a space to distinguish the word Characters each English letter is capitalized  android:cursorvisible= "true"//set cursor to show or hide, default to show  android:digits= "0123456789.abc# "//sets what characters are allowed to be entered  android:drawabletop//the output of a drawable android:drawablebottom//directly above the text Output a drawable android below text::d rawableleft//output A drawable android to the left of text::d rawableright// Output a drawable android::editable= "true"//setting to the right of text if editable  android::ellipsize= "start"//sets how the control is displayed when the text is too long. The start ellipsis is displayed at the beginning; The end ellipsis is displayed at the end; The middle ellipsis is displayed in the middle; marquee is displayed  android:hint= "Please enter the content! "//Set the default prompt that appears in EditText  android:inputtype=" None "sets the type of text that is used to help the input method display the appropriate keyboard type. The parameters can be set as follows: None, text, textcapcharacters uppercase, Textcapwords Word capitalization, textcapsentences only the first letter size, Textautocorrect, Textautocomplete Auto-complete, textmultiline multi-line input, Textimemultiline Input method Multiline, textnosuggestions not prompt, TextemailadDress email address, textemailsubject message subject, Textshortmessage SMS, textlongmessage long message, textpersonname name, textpostaladdress address, Textpassword password, textvisiblepassword visible password, textwebedittext as Web Form text, Textfilte text filter filter, textphonetic pinyin input, numbersigned symbol number format, numberdecimal floating point format with decimal point, phone phone number, datetime date, date, Time  android:numeric= "integer" Set input numeric type, integer integer, Decimal Decimal, signed signed integer  android:textcolor = "#ff8c00"//Set Font color  android:textstyle= " Bold "//Set font type, bold in boldface, italic italic, bolditalic android:textsize=" 20dip "//Set Font size  android:textscalex=" 1.5 " Set word spacing  android:singleline= "true"//Set single-line input mode, text cannot be wrapped automatically   2.enter key icon settings in Android, the soft keyboard's Enter key icon is displayed by default The "Finish" text, shown in 1.   Figure 1 Default soft keyboard   Imagine that when we completed the input in EditText and wanted to search with the input as a keyword, we needed to press the Enter button of the "Done" icon, which obviously did not match the good user experience design.   So, how do you change the icon of the Enter button? Android provides us with the android:imeoptions to achieve this function. The common parameters of  android:imeoptions are as follows: Normal (normal), actionunspecified (unspecified), Actionnone (no action), Actiongo (GO), Actionsearch (search), actionsend (send), Actionnext (next), Actiondone (complete), FlagnoExtractui,flagnoaccessoryaction,flagnoenteraction and so on.   3. In the EditText input QQ expression picture in the EditText input QQ expression picture, need to use to Spannablestring class and Imagespan class. The  spannablestring class inherits from the Android.text.Spanned, which is the addition and separation of immutable textual content. The common methods of the Spannablestring class are the following:  mspannablestring.charat (int i);//Returns the byte  mspannablestring.getchars at the specified index I ( int start, int end, char[] dest, int off),//Gets the string from start to end  mspannablestring.getspans (int querystart, int queryend , Class kind);  mspannablestring.length ();//Returns the number of bytes  mspannablestring.setspan (Object what, int start, int end, int flags);// mspannablestring.tostring ()//return string The  imagespan class is primarily used to implement a picture to replace the specified text.   The specific implementation method is as follows:  spannablestring1 Bitmap mbitmap = Bitmapfactory.decoderesource (Getresources (), resourceId); 2 Imagespan Mimagespan = new Imagespan (mainactivity.this, Mbitmap); 3 spannablestring mspannablestring = new Spannablestring ("QQ"); 4 Mspannablestring.setspan (Mimagespan, 0, 2, spannable.span_exclusive_exclusive); 5 Medittext.append (mspannablestring); SpanFor detailed usage of nablestring, see:  [ 4.edittext input Check   in normal application, when the user enters the obvious error content in the EditText, but chooses "commits", in order to satisfy the good user experience, A friendly message like "Please enter the correct content" should pop up.   How does this function work? Android provides us with the Medittext.seterror () method to implement this feature. First, you need to use the Medittext.gettext () method to get the content in the EditText, then judge its content, and finally, the results are shown by the Medittext.seterror () method.   The specific implementation method is as follows:  edittext.seterror () 1 String mstring =  medittext2.gettext (). toString (); 2 if (mstring = = NULL || Mstring.trim (). Equals ("")) {3 Medittext2.seterror ("Please enter content! 4 return;5} 5.edittext Use instance   In this example, the function of inputting QQ expression picture and edittext input content in EditText is realized. In instance 1, each time you press the "Enter QQ expression" button, you can enter a random QQ expression image in the EditText. In instance 2, if nothing is entered in EditText, pressing the "submit" button will pop up "Please enter the content!" "Prompt for information.     example source code is as follows: &NBSP;MAINACTIVITY.JAVA1 package com.example.android_edittext;23 Import Java.lang.reflect.field;4 Import java.util.random;5 Import android.os.bundle;6 import android.app.activity;7 Import Android.graphics.bitmap;8 Import android.graphics.bitmapfactory;9 Import android.text.spannable;10 Import Android.text.spannablestring;11 Import android.text.style.imagespan;12 Import android.view.view;13 import android.widget.button;14 Import android.widget.edittext;1516 public class Mainactivity extends Activity {1718     private EditText MEDITTEXT_QQ = NULL;           &NBSP;//QQ expression input Display box     private Button mbutton_qq = null;                 &NBSP;//QQ expression input button     private EditText Medittext_check = NULL;    //content check box     private Button mbutton_check = null;          //submit button 2223     @Override24     public void onCreate (Bundle savedins Tancestate) {        super.oncreate (savedinstancestate);         SETCONTENTV Iew (R.layout.activity_main), 2728         MEDITTEXT_QQ = (EditText) This.findviewbyid (r.id.edittext _QQ)         MBUTTON_QQ = (Button) This.findviewbyid (R.ID.BUTTON_QQ); 30 &nbsp       Medittext_check = (EditText) This.findviewbyid (R.id.edittext_check),         Mbutton_check = (Button) This.findviewbyid (R.id.button_check) 3233        //QQ expression input button monitoring nbsp     Mbutton_qq.setonclicklistener (new View.onclicklistener () {+             Publ IC void OnClick (View v) {                int randomid = 1 + new Random (). Nextin T (6);              //random number 1-637                 try {38                     Field field = R.drawable.class.getdeclaredfield ("QQ +randomid                     int resourceId = Integer.parseint (fie Ld.get (NULL). ToString ());      //Get Picture resources ID40                     BITMAPMbitmap = Bitmapfactory.decoderesource (Getresources (), resourceId),               &N Bsp     Mimagespan = new Imagespan (Mainactivity.this, Mbitmap), Imagespan           &NBSP ;         spannablestring mspannablestring = new Spannablestring ("QQ"),         &N Bsp           Mspannablestring.setspan (Mimagespan, 0, 2, spannable.span_exclusive_exclusive); 44 & nbsp                   Medittext_qq.append (mspannablestring),     &NBS P          } catch (Exception e) {4647                }48            }49        }) 5051        //submit Button Monitor 52         Mbutton_check.setonclicklistener (new View.onclicklistener () {$         & nbsp &nbSp public void OnClick (View v) {                String mstring =  medittext_c Heck.gettext (). ToString (),                 if (mstring = = NULL | | Mstring.trim (). Equals ("")) {             //judgment EditText content is empty,           &NBS P         Medittext_check.seterror ("Please enter the content! ")                     return;58           & nbsp    }59            }60        });    }6263}act Ivity_main.xml 1 <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"  2     xmlns:tools= "Http://schemas.android.com/tools"  3     android:orientation= "vertical"  4     android:layout_width= "match_parent"  5     Android:layout_height= "Match_parent" > 6     7     <!--edittext input QQ emoji image--> 8     < Textview 9         android:layout_margintop= "5DP"         Android:layout_ Width= "Match_parent"         android:layout_height= "wrap_content"         android:text= "@string/string_textview1"    >13     </textview>14    15   & nbsp <edittext16         android:id= "@+id/edittext_qq" +         android:hint= "@ String/string_edittext         android:layout_width= "match_parent"         android:layout_height= "Wrap_content"         android:layout_margintop= "5DP"        >21     </edittext>22    23     &LT;BUTTON24         Android Oid:id= "@+id/button_qq"         android:layout_width= "match_parent"         android:layout_height= "Wrap_content"         android:text= "@string/string_button1"    >28     </button>29    30     <!--edittext input check-->31     <textview32         an droid:layout_margintop= "20DP"         android:layout_width= "match_parent"         android:layout_height= "wrap_content"         android:text= "@string/string_textview2"    >36     </textview>37    38     <edittext39       &N Bsp Android:id= "@+id/edittext_check"         android:layout_width= "match_parent"         android:layout_height= "wrap_content"         android:digits= "0123456789"   & nbsp     android:inputtype= "number|textcapcharacters    >   44     </edittext>45    46     < Button47         android:id= "@+id/button_check" +         android:layout_width= " Match_parent "        android:layout_height=" Wrap_content ",         Android: text= "@string/string_button2"   >   51     </button>52    53 </ Linearlayout>

[Go]android Learning: How to use EditText

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.