[Android training video series] 1.4 starting another activity

Source: Internet
Author: User

1. Main Content
In this section, we will continue to improve the Code on the basis of section 1.3, write the button RESPONSE event, and create an intent in the response event to start another activity.
2. Video description
Http://www.eyeandroid.com/thread-11210-1-1.html

3. Translation Reference

 

Respond to the send button-response send button

 

The on-click (click) event of the response button. Open the main. xml layout file and add the Android: onclick attribute to the button element:

<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/button_send"
Android: onclick = "sendmessage"/>

Android: onclick attribute value: sendmessage is the name of the method triggered when you click your screen button.

Add the corresponding method in the myfirstactivity class:

/** Called when the user clicks the send button */
Public void sendmessage (view ){
// Perform corresponding button operations
}
 

Note: In eclipse, you can press Ctrl + Shift + O to import missing classes (CMD + Shift + O is used in MAC)

Note that in order for the system to match this method (the sendmessage method you just added in myfirstactivity) with the method names provided in the Android: onclick attribute, their names must be consistent, in particular, this method must meet the following conditions:

  • Public
  • No return value
  • There is a unique view parameter (this view is the view to be clicked)

Next, you can write the code to read the text content in this method and upload the content to another activity.


Build an intent-build an intent (intent)

Intent (intent) is the object (such as two activities) that provides runtime connections in different components ). Intent (intent) represents an application "what do you want to do", you can use it to do a variety of tasks, but most of the time they are used to start another activity. Create an intent (intent) in the sendmessage () method and start the activity named displaymessageactivity:

Intent intent = new intent (this, displaymessageactivity. Class );

The intent constructor has two parameters: the first parameter is context (context) (all of which can use this because the current activity (myfirstactivity) is a subclass of context) the system needs to pass the class Object of the intent application component (in this case, this activity should be started)

Note: If you are using an IDE similar to eclipse, an error will be reported for the displaymessageactivity reference because this class does not exist. Pay attention to this error and you will soon create this class.

An intent (intent) not only allows you to start another activity, but also transfers a data packet to another activity. OK, the edittext element is obtained using the findviewbyid () method, then add its information to intent (intent ):

Intent intent = new intent (this, displaymessageactivity. Class );
Edittext = (edittext) findviewbyid (R. Id. edit_message );
String message = edittext. gettext (). tostring ();
Intent. putextra (extra_message, message );

Sending an intent to other apps-send intent (intent) to other apps)

The intent (intent) created in this lesson contains a very clear intent because it specifies the precise app (Application) component required by an intent; however, when intent does not specify a clear component, intent (intent) is implicit, but it allows any application installed on the device to respond as long as the application meets the requirements of each intent (intent) for more information, see the interacting with other apps course.

Intent (intent) can pass a variety of sets that appear in the form of key-value pairs. It can be called extras. The putextra () method uses the character escape as its key, the second parameter is its value. To obtain extra (additional) data in the next activity, you should define a public constant as the key (key), OK, define a constant named extra_message at the top of the myfirstactivity class:

Public class myfirstactivity extends activity {
Public final static string extra_message = "com. example. MyApp. Message ";
...
}

To make the extras key unique, using the package name of your application as the prefix of the extras key is a good practice, because your application may need to interact with other applications.


Start the second activity-start the second activity

To start an activity, you only need to call the startactivity () method and pass in your intent (intent) system to receive your request. Then, the activity specified in intent will be instantiated, the complete sendmessage () method that is called by the send (send) button contains this method is now like this:

/** Called when the user clicks the send button */
Public void sendmessage (view ){
Intent intent = new intent (this, displaymessageactivity. Class );
Edittext = (edittext) findviewbyid (R. Id. edit_message );
String message = edittext. gettext (). tostring ();
Intent. putextra (extra_message, message );
Startactivity (intent );
}

Now you need to create a displaymessageactivity Support Program for execution.


Create the second activity-create the second activity

In your project, create a class named displaymessageactivity. Java in the src/<package-Name>/path.

Note: In eclipse, right-click src/path, select new> class, enter displaymessageactivity, and specify to inherit Android. App. activity.

In this class, add the oncreate () callback method:

Public class displaymessageactivity extends activity {
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
}
}

All activity subclasses must implement the oncreate () method. This method is called when the system creates an activity instance. This method defines the activity layout and initializes necessary activity components.

 


Add it to the manifest-add activity to the manifest File

You must go to the manifest file, androidmanifest. use the <activity> element in XML to declare all your activities. displaymessageactivity is called by a clear intent (intent), so it does not need any intent filters (intent filter) (intent filters, you can see where the myfirstactivity is declared in the manifest file) so that displaymessageactivity can use a very simple code declaration in the <Application> element;

<Application...>
<Activity Android: Name = "com. example. MyApp. displaymessageactivity"/>
...
</Application>
 

This app can be run now, because the intent in the first activity can now parse the displaymessageactivity class. If you are running the app now, click the send button to start it, the second activity does not display anything;


Receive the intent-Get intent (intent)

For each activity called by intent, you can use the getintent () method to obtain the data contained in intent and intent in the started activity, no matter where you navigate it. In the oncreate () method of the displaymessageactivity class, get the additional information provided by intent and myfirstactivity:

Intent intent = getintent ();
String message = intent. getstringextra (myfirstactivity. extra_message );


Display the message-display information

Display Information on the screen, create a textview component, set its value using settext (), and use the setcontentview () method to use textview as the root) the layout of the view added to the activity.

The complete oncreate () method of displaymessageactivity now looks as follows:

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
 
// Obtain information from intent
Intent intent = getintent ();
String message = intent. getstringextra (myfirstactivity. extra_message );
 
// Create a textview object
Textview = new textview (this );
Textview. settextsize (40 );
Textview. settext (Message );
 
Setcontentview (textview );
}

Now you can run the app, enter information in the text, and click the send button, OK. Now you can see the information on the second activity.

The two activities are in the same application and run on android4.0.

 
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.