My Android notes (3) -- use Intent to pass values between activities

Source: Internet
Author: User

In Android, an Activity can be understood as a screen. Intent is used to switch a program from one Activity to another.

Intent is responsible for interaction and communication between Android programs and between activities and services in the program.

 


The following is a simple demo, which uses Intent to switch from one Activity to another, and transmits a data for display.

 


Take a look --

First Activity:

 
Enter the name "Barry ":

Click "OK" to jump to another Activity and display "Hello, Barry !" :

 
The Code is as follows:

 

Demo_intentActivity.java

 

[Java]
Package barry. android. intent;
 
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
 
Public class Demo_intentActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
String yourName = (EditText) findViewById (R. id. name_text ))
. GetText (). toString ();
Intent intent = new Intent ();
Intent. setClass (Demo_intentActivity.this, RecordActivity. class );
Intent. putExtra ("name", yourName); // set the data to be passed in Intent
StartActivity (intent); // start a new Activity
Demo_intentActivity.this.finish (); // end the Activity.
}
});
}
}
Package barry. android. intent;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;

Public class Demo_intentActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Button btn = (Button) findViewById (R. id. button1 );
Btn. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
String yourName = (EditText) findViewById (R. id. name_text ))
. GetText (). toString ();
Intent intent = new Intent ();
Intent. setClass (Demo_intentActivity.this, RecordActivity. class );
Intent. putExtra ("name", yourName); // set the data to be passed in Intent
StartActivity (intent); // start a new Activity
Demo_intentActivity.this.finish (); // end the Activity.
}
});
}
} And its 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">
 
<EditText
Android: id = "@ + id/name_text"
Android: hint = "enter your name"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>

<Button android: text = "OK"
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
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">

<EditText
Android: id = "@ + id/name_text"
Android: hint = "enter your name"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>

<Button android: text = "OK"
Android: id = "@ + id/button1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"/>

</LinearLayout>

 


Another Activity RecordActivity. java

[Java]
Package barry. android. intent;
 
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;
 
Public class RecordActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. record );
Intent intent = getIntent ();
String yourname = intent. getStringExtra ("name"); // get data from Intent
TextView text_show = (TextView) findViewById (R. id. text_show );
Text_show.setText ("hello," + yourname + "! "); // Display to the screen
}
}
Package barry. android. intent;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. widget. TextView;

Public class RecordActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. record );
Intent intent = getIntent ();
String yourname = intent. getStringExtra ("name"); // get data from Intent
TextView text_show = (TextView) findViewById (R. id. text_show );
Text_show.setText ("hello," + yourname + "! "); // Display to the screen
}
}
And its layout file record. 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: id = "@ + id/text_show"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>
 
</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: id = "@ + id/text_show"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>

</LinearLayout>

Note that each Activity must be declared in AndroidManiFest. xml. Therefore, you must add the following to AndroidManiFest. xml:


[Html]
<Activity android: name = ". RecordActivity"> </activity>
 
From Wolf's second nest
 

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.