Android game development system control-EditText
EditText (input box) is a common component used to interact with users. It is often used when logging on to a game and entering account and password information.
Create the project "EditTextProject"
Function: Enter the content in the input box and display it in TextView.
Project running result:
The project source code is as follows:
Main. xml (layout file)
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: id = "@ + id/TV"
/>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: hint = "prompt message"
Android: id = "@ + id/et"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Get EditText content! "
Android: id = "@ + id/btn"
/>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
Android: id = "@ + id/TV"
/>
<EditText
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: hint = "prompt message"
Android: id = "@ + id/et"
/>
<Button
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "Get EditText content! "
Android: id = "@ + id/btn"
/>
</LinearLayout>
String. xml
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, EditTextProject </string>
<String name = "app_name"> EditTextProject </string>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, EditTextProject </string>
<String name = "app_name"> EditTextProject </string>
</Resources>
EditTextProject. java
[Java]
Package com. editText;
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 EditTextProject extends Activity implements OnClickListener {
Private EditText et; // creates an object for text editing.
Private Button btn;
Private TextView TV;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Et = (EditText) findViewById (R. id. et); // edit the instantiated text
Btn = (Button) findViewById (R. id. btn );
Btn. setOnClickListener (this); // register the listener
TV = (TextView) findViewById (R. id. TV );
}
Public void onClick (View v ){
If (v = btn ){
// Obtain the text content in EditText
String str = et. getText (). toString ();
// Let TextView display the obtained EditText content str
TV. setText (str );
}
}
}
Author: wwj