Android learning route (5) enable another Activity, androidactivity

Source: Internet
Author: User

Android learning route (5) enable another Activity, androidactivity

After completing the previous course, you already have an application. This application shows an activity that contains a text box and a button (a separate interface ). In this course, you will add some code to MainActivity so that you can jump to another activity when you click Send.

Send Response button

To respond to the button clicking event, openfragment_main.xmlLayout the file, and then<Button>Add to elementandroid:onClickAttribute:

<Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_send"    android:onClick="sendMessage" />

Thisandroid:onClickAttribute value,"sendMessage"Is the name of a method in your activity. When you click the button, this method is called.

OpenMainActivityClass, add the corresponding method:

/** Called when the user clicks the Send button */public void sendMessage(View view) {    // Do something in response to button}

To allow the system to matchandroid:onClickThe method corresponding to the Method Name of the assigned value. The signature must be displayed strictly. (The signature must be exactly as shown.) the method must be as follows:

  • Is Public
  • The return type is void.
  • Only oneViewType parameter (it indicatesView)

Next, you need to fill up this method. To read the content in the text box, and then pass the content to another activity.

Construct an Intent

IntentIs an object that is bound at runtime by two separated components (for example, two activities.IntentIndicates that an application "intends to do something ". You can use intent to do many things, but it is most commonly used to open another activity.

InsendMessage()CreateIntentTo enableDisplayMessageActivityActivity:

Intent intent = new Intent(this, DisplayMessageActivity.class);

This requires pilot ImportIntentClass:

import android.content.Intent;

TIPS:In Eclipse, you can use Ctrl + Shift + O to import unimported classes (Cmd + Shift + O in Mac ).

The constructor used here contains two parameters:

  • The first parameter isContext(Used herethisBecauseActivityClass isContextSubclass)
  • The second parameter is to be passedIntentApplication Component class (in this example, the activity to be opened)
Send intent to other applications

The intent created in this application is a clear intent, because izhgeIntentSpecifies the explicit application component that intent should provide. The Intent can also be concealed. In some cases, the intent does not specify components, however, it allows applications installed on devices to respond to the specified Intent as parameters. For more information, see the course: Interacting with Other Apps.

Tip: DisplayMessageActivityWill cause an error, because you are using an IDE such as Eclipse, it will find that this class does not exist. Ignore this error and you will immediately create this class.

Intent can not only open another activity, but also carry a bundle containing data. InsendMessage()Internal method, usefindViewById()To obtainEditTextElement, and add its value to the intent:

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);

Tip:You need to importandroid.widget.EditText. You need to defineEXTRA_MESSAGEConstant.

IntentA set of different types of key-value pairs called extars can be carried.putExtra()The first parameter is the key name, and the second parameter is the value.

To enable the next activity to find extra data, you should use public constants to assign values to your intent key. Therefore, add a definition in the MainActivity header.EXTRA_MESSAGE:

public class MainActivity extends ActionBarActivity {    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";    ...}

When your application interacts with other applications, using the package name of your application as the key defined by intent extras is a good choice.

Open the second Activity

To open an activity, callstartActivity()Method, andIntentAfter the system accepts the call, the specified intentActivity.

In the new code, the completesendMessage()The method should be as follows:

/** Called when the user clicks the Send button */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);}

Now you need to createDisplayMessageActivityClass to continue working.

Create the second Activity

Figure 1. Create Activity wizard in Eclipse

Use Eclipse to create an activity:

  1. ClickNew.
  2. In the displayed window, openAndroidFolder and selectAndroid ActivityAnd then clickNext.
  3. SelectBlankActivityClickNext.
  4. Full Activity details:
    • Project: MyFirstApp
    • Activity Name: DisplayMessageActivity
    • Layout Name: Activity_display_message
    • Fragment Layout Name: Fragment_display_message
    • Title: My Message
    • Hierarchial Parent: Com. example. myfirstapp. MainActivity
    • Navigation Type: None

    ClickFinish.

If you are not using Eclipse or the command line toolsrc/Create the DisplayMessageActivity class under the directory, and thenMainActivity.java.

OpenDisplayMessageActivity.javaFile, if you use Eclipse:

  • This class already containsonCreate()Method implementation. You will update this implementation later.
  • There is alsoonCreateOptionsMenu()Method implementation, but you will not use it in this example, you can delete it.
  • AndonOptionsItemSelected()Method, which is used to processUpEvent to keep it as it is.
  • There is also a class that inherits the Fragment classPlaceholderFragmentClass. You will not use this Activity.

Fragments splits methods and UIS into reusable modules.

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.