Android [elementary tutorial] Article 2 edittext Control

Source: Internet
Author: User

In the next tutorial, we will add edittext on the interface. What is edittext? You can see the name. What? You didn't learn English well in elementary school. I was dizzy. Edit means editing. text means text, and the text control can be edited together. Let's take a look at main. xml.

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <edittext Android: layout_height = "wrap_content" Android: layout_width = "match_parent" Android: Id = "@ + ID/edittext"> </edittext> <button Android: layout_width = "match_parent" Android: layout_height = "wrap_content" Android: text = "button" Android: Id = "@ + ID/button"> </button> <textview Android: layout_height = "wrap_content" Android: layout_width = "fill_parent" Android: text = "@ string/Hello" Android: Id = "@ + ID/text"> </textview> </linearlayout>

Then there is the code in the activity:

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. textview; public class buttondemoactivity extends activity implements onclicklistener {private textview text = NULL; private edittext edit_text = NULL;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // find main by ID. textview control text = (textview) findviewbyid (R. id. text); // find main by ID. edittext control edit_text = (edittext) findviewbyid (R. id. edittext); // find main by ID. button button = (button) findviewbyid (R. id. button); // Add and click the listener button for the button control. setonclicklistener (this);}/*** for Main. click to listen to all controls in XML. Of course, you must register the controls for listening * example: button. setonclicklistener (this); * // @ overridepublic void onclick (view v) {updatetext ();} private void updatetext () {// obtain the text information entered in edittext string edit_str = edit_text.gettext (). tostring (); // set text information to the textview control to display text. settext (edit_str );}}

Let's add one more point this time. OK, after writing, or run it. Will it be interesting if you click the input information button? What? Cannot run? That must have been an error. You can check it again (Look, Look ). well, you must have written it here. Oh, no. We need to add more information. it is also very simple. We need to make some judgments on the input information. You can't lose anything, right? You have to lose something to get your head!

Modify the bottom updatetext (). The Code is as follows:

Private void updatetext () {// obtain the text information entered in edittext string edit_str = edit_text.gettext (). tostring (); If (edit_str.trim (). length () = 0) {text. settext ("You have nothing to lose? What do you want me to display? ") ;}Else {// set the text information to the textview control to display text. settext (edit_str );}}

OK. Now, this tutorial is over. The edittext control is also very simple. This is a very important control. Remember it!

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.