Introduction and Application of edittext
Overall process:
Add the following code in widgetdemo. Java:
Add the following two sentences to the find_and_modify_button () method:
Button edit_view_button = (button) findviewbyid (R. Id. edit_view_button); <br/> edit_view_button.setonclicklistener (edit_view_button_listener); </P> <p>
Private button. onclicklistener edit_view_button_listener = new button. onclicklistener () {<br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (widgetdemo. this, edittextactivity. class); <br/> startactivity (intent); <br/>}< br/> };
Code in edittextactivity. Java:
Package COM. example. widgetdemo; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. button; </P> <p> Import android. widget. edittext; </P> <p> public class edittextactivity extends activity {<br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settitle ("edittextactivity"); <br/> setcontentview (R. layout. editview); <br/> find_and_modify_text_view (); <br/>}</P> <p> private void find_and_modify_text_view () {<br/> button get_edit_view_button = (button) findviewbyid (R. id. get_edit_view_button); <br/> get_edit_view_button.setonclicklistener (get_edit_view_button_listener); <br/>}</P> <p> private button. onclicklistener get_edit_view_button_listener = new button. onclicklistener () {<br/> Public void onclick (view v) {<br/> edittext edit_text = (edittext) findviewbyid (R. id. edit_text); <br/> charsequence edit_text_value = edit_text.gettext (); <br/> settitle ("edittext value:" + edit_text_value ); <br/>}< br/>}; </P> <p>}
Code in editview. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> </P> <p> <edittext <br/> Android: TEXT = "Please input:" <br/> Android: Id = "@ + ID/edit_text" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </edittext> <br/> <button <br/> Android: TEXT = "require the value of editview" <br/> Android: Id = "@ + ID/get_edit_view_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button> <br/> </linearlayout> <br/>
And the code added in manifest:
</Activity> <br/> <activity Android: Name = "textactivity"> <br/> </activity>
Running result:
Enter Text in edittext:
Click the button to display the value of editview, and the value is displayed on the title:
Specific implementation process:
First, add a button named "edittext" on the main portal interface of the program, and set its listener to open the edittext interface when an event is received, the code for adding a button to an XML file is as follows:
<Button <br/> Android: text = "editview" <br/> Android: Id = "@ + ID/edit_view_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button>
Code Description: define a button with ID edit_view_button and set it to editview. The width and height are adjusted according to the content.
The response is as follows:
Private button. onclicklistener edit_view_button_listener = new button. onclicklistener () {<br/> Public void onclick (view v) {<br/> intent = new intent (); <br/> intent. setclass (widgetdemo. this, edittextactivity. class); <br/> startactivity (intent); <br/>}< br/> };
Code explanation:
Click the button to jump to the next activity, that is, the edittextactivity.
The oncreate () method code is as follows:
@ Override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> settitle ("edittextactivity"); <br/> setcontentview (R. layout. editview); <br/> find_and_modify_text_view (); <br/>}
Code explanation:
Specify the template file as the editview component and add the following code:
<Edittext <br/> Android: text = "Please input:" <br/> Android: Id = "@ + ID/edit_text" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </edittext>
To display the value of editview to the title, you need to add a button and add the response method for clicking the button in edittextactivity. java.
Add a button in XML
<Button <br/> Android: text = "require the value of editview" <br/> Android: Id = "@ + ID/get_edit_view_button" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content"> <br/> </button>
And the code for its click response:
Private void find_and_modify_text_view () {<br/> button get_edit_view_button = (button) findviewbyid (R. id. get_edit_view_button); <br/> get_edit_view_button.setonclicklistener (get_edit_view_button_listener); <br/>}</P> <p> private button. onclicklistener get_edit_view_button_listener = new button. onclicklistener () {<br/> Public void onclick (view v) {<br/> edittext edit_text = (edittext) findviewbyid (R. id. edit_text); <br/> charsequence edit_text_value = edit_text.gettext (); <br/> settitle ("edittext value:" + edit_text_value ); <br/>}< br/> };
Obtain the edittext value and display it on the title.