First, check the main layout file main. xml:
[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 = "Enter the first number:"/>
<EditText
Android: id = "@ + id/editText1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>"
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "enter the second number:"/>
<EditText
Android: id = "@ + id/editText2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/button"
Android: text = "Submit"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>"
</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 = "Enter the first number:"/>
<EditText
Android: id = "@ + id/editText1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>"
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "enter the second number:"/>
<EditText
Android: id = "@ + id/editText2"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/button"
Android: text = "Submit"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>"
</LinearLayout>
The content of the main Activity file is as follows:
[Java]
Package com. liuhaoyu;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Public class MainActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final EditText edit1 = (EditText) findViewById (R. id. editText1 );
Final EditText edit2 = (EditText) findViewById (R. id. editText2 );
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
String num1 = edit1.getText (). toString ();
String num2 = edit2.getText (). toString ();
Intent intent = new Intent (MainActivity. this, SecondActivity. class );
Bundle bundle = new Bundle ();
Bundle. putCharSequence ("number1", num1 );
Bundle. putCharSequence ("number2", num2 );
Intent. putExtras (bundle );
StartActivity (intent );
}
});
}
}
Package com. liuhaoyu;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. Button;
Import android. widget. EditText;
Public class MainActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Final EditText edit1 = (EditText) findViewById (R. id. editText1 );
Final EditText edit2 = (EditText) findViewById (R. id. editText2 );
Button button = (Button) findViewById (R. id. button );
Button. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
String num1 = edit1.getText (). toString ();
String num2 = edit2.getText (). toString ();
Intent intent = new Intent (MainActivity. this, SecondActivity. class );
Bundle bundle = new Bundle ();
Bundle. putCharSequence ("number1", num1 );
Bundle. putCharSequence ("number2", num2 );
Intent. putExtras (bundle );
StartActivity (intent );
}
});
}
}
In the button listening function, a Bundle object is created to save the value entered by the user. Then, the Bundle is passed to SecondActivity as an additional parameter of Intent.
The implementation of SecondActivity is as follows:
[Java]
Package com. liuhaoyu;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class SecondActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. second );
Int sum;
Intent intent = getIntent ();
Bundle bundle = intent. getExtras ();
String num1 = bundle. getString ("number1 ");
String num2 = bundle. getString ("number2 ");
Sum = Integer. parseInt (num1) + Integer. parseInt (num2 );
TextView text = (TextView) findViewById (R. id. textView );
Text. setText (num1 + "+" + num2 + "=" + String. valueOf (sum ));
}
}
Package com. liuhaoyu;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
Public class SecondActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. second );
Int sum;
Intent intent = getIntent ();
Bundle bundle = intent. getExtras ();
String num1 = bundle. getString ("number1 ");
String num2 = bundle. getString ("number2 ");
Sum = Integer. parseInt (num1) + Integer. parseInt (num2 );
TextView text = (TextView) findViewById (R. id. textView );
Text. setText (num1 + "+" + num2 + "=" + String. valueOf (sum ));
}
}
In the onCreate function, the Intent is obtained through getIntent (), and the Bundle passed by MainActivity is obtained through Intent. getExtras (), so that the passed value can be obtained.
The layout file of SecondActivity is as follows:
[Html]
Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/textView"
Android: textSize = "20dp"
Android: layout_gravity = "center"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>"
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/textView"
Android: textSize = "20dp"
Android: layout_gravity = "center"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>"
</LinearLayout>
Finally, add the SecondActivity statement to the AndroidManifest. xml file:
[Html]
<Activity
Android: icon = "@ drawable/ic_launcher"
Android: name = ". SecondActivity"
Android: label = "calculation result"
Android: theme = "@ android: style/Theme. Dialog"
>
</Activity>