Android edittext Detailed and sample code _android

Source: Internet
Author: User

The structure of EditText in the API

Java.lang.Object

Android.view.View

Android.widget.TextView

Android.widget.EditText

 Known Direct subclass:

Autocompletetextview, Extractedittext

Known indirect subclass:

Multiautocompletetextview

EditText is a direct subclass of TextView so EditText will inherit some of the methods of the parent class TextView. Below I write a demo with my own and everybody detailed explain the use method of EditView.

1. Simple EditText input Box

Very simply, configure the EditText in the layout layout to get the content entered in the current EditView and display it when configuring a button to listen for the button in code.

xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:id=" @+id/textviewll "android:orientation=" vertical "android:layout_width=" 
  Fill_ Parent " 
  android:layout_height=" fill_parent "> 
  <edittext 
   android:id=" @+id/sample_edit_text0 " 
   android:layout_width= "fill_parent" 
   android:layout_height= "wrap_content" 
   android:text= " Simple edittext input Box "/> 
  <button 
   android:id=" 
   @+id/sample_button0 "android:layout_width=" Fill_ Parent "android:layout_height=" wrap_content 
   android:text= "OK"/> 
</LinearLayout> 

Java code

public class Sampleactivity extends activity { 
  @Override 
  protected void onCreate (Bundle savedinstancestate) { C3/>setcontentview (r.layout.sample); 
  
  Final EditText editText0 = (edittext) Findviewbyid (r.id.sample_edit_text0); 
  
  Button Button0 = (button) Findviewbyid (r.id.sample_button0); 
  
  Button0.setonclicklistener (New Onclicklistener () { 
  
    @Override public 
    void OnClick (View arg0) { 
    String str = Edittext0.gettext (). toString (); 
    Toast.maketext (Sampleactivity.this,str, Toast.length_long). Show (); 
    } 
  ); 
  
  Super.oncreate (savedinstancestate); 
  } 
 

2. Limit the contents of the EditText input box

Configuring information in Layout

Android:digits= "1234567890.+-*/%\n ()"

Restrict input boxes to enter only their own definition of the string if you enter another will not be displayed

Android:phonenumber= "true"

Only cell phone number can be entered in the Limit input box

Android:password= "true"

Any content entered in the Limit input box will be displayed with a "*" Symbol

android:hint= "Default Text"

Text that appears in the input box by default before you enter the content

Android:textcolorhint= "#FF0000 ″

Set text content Color

Android:enabled= "false"

Set input box cannot be edited

3. Display picture in edit box

The previous talk TextView in the TextView to add a picture of the method, because EditText is TextView subclass, so of course can also add pictures, but once in the EditText add a picture can not be deleted, As shown, I can edit the content next to the picture and write the text.

xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/textviewll "android:orientation=" vertical "android:layout_width=" fill_parent "Androi" d:layout_height= "Fill_parent" > <edittext android:layout_width= "wrap_content" Android:layout_h eight= "Wrap_content" android:text= "android:textcolor=" #FF0000 "android:drawablebottom=" underneath the picture @dra Wable/jay "android:layout_alignparenttop=" true "android:layout_centerhorizontal=" true "> &L t;/edittext> <edittext android:layout_width= "wrap_content" android:layout_height= "Wrap_conten" T "android:text=" is above the picture "android:textcolor=" #FF0000 "android:drawabletop=" @drawable/jay "an 
   Droid:layout_alignparentbottom= "true" android:layout_centerhorizontal= "true" > </EditText> <edittext Android: layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "to the left of the picture" Android:tex Tcolor= "#FF0000" android:drawableleft= "@drawable/jay" android:layout_alignparentleft= "true" Androi D:layout_centervertical= "true" > </EditText> <edittext android:layout_width= "Wrap_cont 
       Ent "android:layout_height=" wrap_content "android:text=" to the right of the picture android:textcolor= "#FF0000" android:drawableright= "@drawable/jay" android:layout_alignparentright= "true" Android:layout_centervertica 
 L= "true" > </EditText> </relativelayout >

4. Set the soft keyboard enter key

As shown in the picture, we can modify the style of the Enter button on the soft keyboard to listen to the button click event in the code.

Java code

Package Cn.m15.xys; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.view.KeyEvent; 
Import Android.view.inputmethod.EditorInfo; 
Import Android.widget.EditText; 
Import Android.widget.TextView; 
Import Android.widget.Toast; 
  
Import Android.widget.TextView.OnEditorActionListener; public class Keyboardactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {s 
  
  Etcontentview (R.layout.keyboard); 
  
  EditText editText0 = (edittext) Findviewbyid (R.ID.TXTTEST0); Edittext0.setoneditoractionlistener (New Oneditoractionlistener () {@Override public boolean oneditoraction (Te Xtview arg0, int arg1, keyevent arg2) {if (arg1 = = Editorinfo.ime_action_go) {Toast.maketext (keyboardactivit 
    Y.this, "You click the soft keyboard ' go ' button", Toast.length_short. Show (); 
    return false; 
  } 
  }); 
  
  EditText editText1 = (edittext) Findviewbyid (R.ID.TXTTEST1); Edittext1.setoneditoractionlistener (New OnediToractionlistener () {@Override public boolean oneditoraction (TextView arg0, int arg1, keyevent arg2) { if (arg1 = = Editorinfo.ime_action_search) {toast.maketext (Keyboardactivity.this, "you click the soft keyboard ' search ' button", Toast.le 
    Ngth_short). Show (); 
    return false; 
  } 
  }); 
  
  EditText editText2 = (edittext) Findviewbyid (R.ID.TXTTEST2); Edittext2.setoneditoractionlistener (New Oneditoractionlistener () {@Override public boolean oneditoraction (Te Xtview arg0, int arg1, keyevent arg2) {if (arg1 = = editorinfo.ime_action_send) {Toast.maketext (keyboardactiv 
    Ity.this, "You point the soft keyboard ' send ' button", Toast.length_short. Show (); 
    return false; 
  } 
  }); 
  
  EditText editText3 = (edittext) Findviewbyid (R.ID.TXTTEST3); Edittext3.setoneditoractionlistener (New Oneditoractionlistener () {@Override public boolean oneditoraction (Te 
 Xtview arg0, int arg1, keyevent arg2) {if (arg1 = = Editorinfo.ime_action_next) {     Toast.maketext (Keyboardactivity.this, "you click the soft keyboard ' next ' button", Toast.length_short). Show (); 
    return false; 
  } 
  }); 
  
  EditText EDITTEXT4 = (edittext) Findviewbyid (R.ID.TXTTEST4); Edittext4.setoneditoractionlistener (New Oneditoractionlistener () {@Override public boolean oneditoraction (Te Xtview arg0, int arg1, keyevent arg2) {if (arg1 = = Editorinfo.ime_action_done) {Toast.maketext (keyboardactiv 
    Ity.this, "You click on the soft keyboard ' finish ' button", Toast.length_short. Show (); 
    return false; 
  } 
  }); 
  
  EditText EDITTEXT5 = (edittext) Findviewbyid (R.ID.TXTTEST5); Edittext5.setoneditoractionlistener (New Oneditoractionlistener () {@Override public boolean oneditoraction (Te Xtview arg0, int arg1, keyevent arg2) {if (arg1 = = editorinfo.ime_action_unspecified) {Toast.maketext (Keyboa 
    Rdactivity.this, "You ordered the soft keyboard ' unspecified ' button", Toast.length_short. Show (); 
    return false; 
  } 
  }); Super.oncreate (SavedInstancestate); 
 } 
}

Monitor the soft keyboard Click event

xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/textviewll "android:orientation=" vertical "android:layout_width=" fill_parent "Android:l" ayout_height= "Fill_parent" > <edittext android:id= "@+id/txttest0" android:imeoptions= "ActionGo" Android:la 
  Yout_width= "Fill_parent" android:layout_height= "Wrap_content" android:hint= "special button-go to" ></EditText> <edittext android:id= "@+id/txttest1" android:imeoptions= "Actionsearch" android:layout_width= "Fill_parent" a ndroid:layout_height= "Wrap_content" android:hint= Special button-search "></EditText> <edittext android:id=" @+id/ TxtTest2 "android:imeoptions=" Actionsend "android:layout_width=" fill_parent "android:layout_height=" Wrap_conte NT "android:hint=" special button-send "></EditText> <edittext android:id=" @+id/txttest3 "android:imeoptions = "Actionnext" Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" android:hint= "special button-Next" ></EditText> ; EditText android:id= "@+id/txttest4" android:imeoptions= "Actiondone" android:layout_width= "Fill_parent" Android : layout_height= "Wrap_content" android:hint= "special button-complete" ></EditText> <edittext android:id= "@+id/txttes T5 "android:imeoptions=" actionunspecified "android:layout_width=" fill_parent "android:layout_height=" Wrap_cont 
 Ent "android:hint=" special button-unspecified "></EditText> </LinearLayout>

5. Key event for listening to soft keyboard

When doing a project sometimes need to do when the user input content to do the test, for example, if the user input illegal content does not show in the EditText, this time I will use the Addtextchangedlistener to listen to the user input state. You can change what the user enters in the listener or prompt the user to enter the content is illegal and so on. As shown in the picture, each input operation can be normally monitored, the user input the normal flow of content beforetextchanged ()-"ontextchanged ()-" aftertextchanged () and then the notification screen drawing displayed on the screen So we can modify the user input or intercept the user input in these three methods.

Java code

Package Cn.m15.xys; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.text.Editable; 
Import Android.text.TextWatcher; 
Import Android.widget.EditText; 
  
Import Android.widget.TextView; 
  public class Monitorkeyactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) { 
  Setcontentview (R.layout.monitorkey); 
  EditText EditText = (edittext) Findviewbyid (r.id.monitor_edit_text0); 
  Final TextView textView0 = (TextView) Findviewbyid (r.id.monitor_text0); 
  Final TextView TextView1 = (TextView) Findviewbyid (R.ID.MONITOR_TEXT1); 
  
  Final TextView textView2 = (TextView) Findviewbyid (R.ID.MONITOR_TEXT2); Edittext.addtextchangedlistener (New Textwatcher () {@Override public void ontextchanged (Charsequence-Text, in  t start, int before, int count) {//text The changed string information in the input box//start the starting position of the changed string in the input box//before the position of the string before the change in the input box The default is the number of input strings changed in the 0//count input box textview1.settext ("After the input string [" + text.)ToString () + "] Start cursor [" + Start +] "Enter quantity [" + count+ "]"); @Override public void beforetextchanged (charsequence text, int start, int count,int after) {//text input The string information before the change in the box//start the starting position of the string before the change in the input box//count the number of strings changed before and after the change in the input box is typically 0//after the offset of the changed string in the input box to the starting position System. 
    Out.println (Text.tostring ()); 
    Textview0.settext ("Enter before string [+ text.tostring () +"] Start cursor ["+ Start +"] End offset ["+ after +"]); @Override public void aftertextchanged (Editable edit) {//edit input ends the information rendered in the input box Textview2.settext (" 
    Enter the end of the content is ["+ edit.tostring () +"] will be displayed on the screen "); 
  
  } 
  }); 
  Super.oncreate (savedinstancestate); 
 } 
}

Xml/html Code

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/textviewll "android:orientation=" vertical "android:layout_width=" fill_parent "Android:l" 
   ayout_height= "Fill_parent" > <textview android:id= "@+id/monitor_text0" android:layout_width= "Fill_parent"  
   android:layout_height= "Wrap_content" android:textsize= "18dip" android:textcolor= "#FF0000"/> <textview Android:id= "@+id/monitor_text1" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" a 
   Ndroid:textsize= "18dip" android:textcolor= "#FF0000"/> <textview android:id= "@+id/monitor_text2" Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:textsize= "18dip" Android:textC 
   Olor= "#FF0000"/> <edittext android:id= "@+id/monitor_edit_text0" android:layout_width= "Fill_parent" Android:layout_height= "Wrap_cOntent "android:hint=" monitors the input status of soft keyboard keys "/> </LinearLayout> 
 

        hope that through this article, we can grasp the knowledge of Android Rdittext, thank you for your support to this site!

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.