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 .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );

// Find the EditText control in main. xml by ID
Edit_text = (EditText) findViewById (R. id. editText );
 
// Find the Button control in main. xml by ID
Button button = (Button) findViewById (R. id. button );
// Add a click listener for the Button control
Button. setOnClickListener (this );
 
}
 
/**
* Click to listen to all the controls in main. xml. Of course, you must register the controls.
* Example: button. setOnClickListener (this );
*/Www.2cto.com
@ Override
Public void onClick (View v)
{
UpdateText ();
 
}
 
Private void updateText ()
{
// Obtain the text information entered in EditText
String edit_str = edit_text.getText (). toString ();
// Set the text information to display in the TextView control.
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 ("do you have nothing to lose? What do you want me to display? ");
} Else
{
// Set the text information to display in the TextView control.
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.

 

From: kangkangz4 Column

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.