[ANDROID5 Series-] 2. Start another activity

Source: Internet
Author: User

Preface

To undertake the previous article,


The next step is to describe the action after clicking the Send button.


Response send button

1. the Res/layout directory. Open activity_my.xml

2. Inside the <Button> element, add the attributeandroid:onClick

Res/layout/activity_my.xml

<button    android:layout_width= "wrap_content"    android:layout_height= "wrap_content"    android:text= " @string/button_send "    android:onclick=" SendMessage "/>

SendMessage-This is the name of the method that is triggered when the button is clicked.

3. Under the Com.oscar999.helloworld package, open the Mainactivity.java file (this is my local Java package and class name)

In this class, add the SendMessage () method, similar to the following:

/** called when the user clicks the Send button */public void sendMessage (view view) {    //does something in response to button
The definition of this method requires the following:

-Must be public

-Return value of void

-View as its only parameter


Building an Intent (Intent)

Intent is a runtime combination that provides two separate components (similar to two activities).

1. The SendMessage method content is added as follows:

public void SendMessage (view view) {  Intent Intent = new Intent (this, displaymessageactivity.class);}
Import Intent Class

Import android.content.Intent;

2. Get the EditText input and put it into the extra of intent

public void SendMessage (view view) {  Intent Intent = new Intent (this, displaymessageactivity.class);  EditText EditText = (EditText) Findviewbyid (r.id.edit_message);  String message = Edittext.gettext (). toString ();  Intent.putextra (Extra_message, MESSAGE);}
Intent can pass values in the form of key-value pairs.


3. Add the definition of extra_message in Java

public class Mainactivity extends Activity {public final static String extra_message = "Com.oscar999.helloworld.MESSAGE";

4. InsendMessage ()method, call the StartActivity () method. Passing Intent parameters

The complete code is as follows:

public void SendMessage (view view) {    Intent Intent = new Intent (this, displaymessageactivity.class);    EditText EditText = (EditText) Findviewbyid (r.id.edit_message);    String message = Edittext.gettext (). toString ();    Intent.putextra (Extra_message, MESSAGE);    StartActivity (intent);}

This basically completes the call to an activity method. The next step is to complete the above mentioneddisplaymessageactivity's activity.

Create a second event (activity)

All child classes of activity must inherit the OnCreate () method. This method is to receive the intent containing the message, and then present the message. The OnCreate () method must use the Setcontentview () method to define the active layout. This is where the activity starts to set up the active component.

To create a step:

1. New---android---Android Activity

2. Select Blank Activity to enter the following page


3. Enter the above section:

Activity name:displaymessageactivity

Layout Name:activity_display_message

Tiltle:my Message

Hierarchical Parent:com.oscar999.helloworld.MainActivity

Click Finish

4. Open Displaymessageactivity.java

This file has three methods

OnCreate ()--Methods to be updated here

Onoptionsitemsselected (), handling the action bar up behavior

Oncreateoptionsmenu (), temporarily unavailable, remove

5. The above action to create activity, in addition to new Java files, but also modified:

Strings.xml



AndroidManifest.xml,


There are slightly differences (paths) generated here and Android 4


Modify the OnCreate () method so that the activity shows the message sent from Mainactivity, and the modified code is as follows:

@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Intent Intent = Getintent (); String message = Intent.getstringextra (mainactivity.extra_message); TextView TextView = new TextView (this); Textview.settextsize (+); textview.settext (message); Setcontentview (TextView) ;/*if (savedinstancestate = = null) {Getfragmentmanager (). BeginTransaction (). Add (R.id.container, new Placeholderfragment ()). commit ();} */}

Run the effect to see ~ ~




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[ANDROID5 Series-] 2. Start another activity

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.