EditText is the most important Android UI component that accepts user input information and can take advantage of edittext.gettext () to get its text content.
Example: Edittextdemo
Operating effect:
Code Listing:
Layout file: Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" Fill_parent "android:stretchcolumns=" 1 " ; <TableRow> <textview android:gravity= "right" android:textstyle= "bold" and roid:padding= "3dip" android:text= "User name:"/> <edittext android:id= "@+id/username "Android:padding=" 3dip "android:scrollhorizontally=" true "/> </TableRow> < tablerow> <textview android:gravity= "right" android:textstyle= "bold" Android :p adding= "3dip" android:text= "User password:"/> <edittext android:id= "@+id/password" android:padding= "3dip" android:password= "true"/> </TableRow> <tablerow Andro Id:gravity= "Right" > &Lt Button android:id= "@+id/cancel" android:text= "Cancel"/> <button Android : id= "@+id/login" android:text= "Login"/> </TableRow></TableLayout>
Java source code file: Mainactivity.java
Package Com.rainsong.edittextdemo;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class Mainactivity extends activity{Button btn_login; Onclicklistener Listener_login; /** called when the activity is first created. */@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Listener_login = new Onclicklistener () {public void OnClick (View v) {EditText username = (Edit Text) Findviewbyid (r.id.username); EditText password = (EditText) Findviewbyid (R.id.password); Toast.maketext (mainactivity.this, "User name:" + username.gettext () + ", User password:" + password.gettext (), Toast. Length_short). Show (); } }; Btn_login =(Button) Findviewbyid (R.id.login); Btn_login.setonclicklistener (Listener_login); }}
API Knowledge Points
Android.widget.EditText
public class
EditText
Extends TextView
Known Direct subclasses
Autocompletetextview, Extractedittext, Searchedittext
Known Indirect subclasses
Multiautocompletetextview
Class Overview
EditText is a thin veneer over TextView, configures itself to be editable.
Public constructors
EditText (Context context)
EditText (context context, AttributeSet attrs)
EditText (context context, AttributeSet attrs, int defstyleattr)
EditText (context context, AttributeSet attrs, int defstyleattr, int defstyleres)
Editable GetText ()
Return the text the TextView is displaying.
Android Common UI Components-EditText