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