Starting Another Activity

  次阅读 来源:互联网(转载协议) 2016-01-14 10:42 我要评论(0)

Respond to the Send Button

To respond to the button's on-click event, open the main.xml layout file and add the android:onClick attribute to the <Button> element:





android:onClick="sendMessage" />

The android:onClick attribute’s value, "sendMessage", is the name of a method in your activity that the system calls when the user clicks the button.

Open the MainActivity class and add the corresponding method:

/** Called when the user clicks the Send button */

public void sendMessage(View view) {

// Do something in response to button


Tip: In Eclipse, press Ctrl + Shift + O to import missing classes (Cmd + Shift + O on Mac).

In order for the system to match this method to the method name given to android:onClick, the signature must be exactly as shown. Specifically, the method must:

Be public

Have a void return value

Have a View as the only parameter (this will be the View that was clicked)

Next, you’ll fill in this method to read the contents of the text field and deliver that text to another activity.

Build an Intent

An Intent is an object that provides runtime binding between separate components (such as two activities). The Intent represents an app’s "intent to do something." You can use intents for a wide variety of tasks, but most often they’re used to start another activity.

Inside the sendMessage() method, create an Intent to start an activity called DisplayMessageActivity:

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

The constructor used here takes two parameters:

A Context as its first parameter (this is used because the Activity class is a subclass of Context)

The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)

Sending an intent to other apps

The intent created in this lesson is what's considered an explicit intent, because the Intent specifies the exact app component to which the intent should be given. However, intents can also be implicit, in which case the Intent does not specify the desired component, but allows any app installed on the device to respond to the intent as long as it satisfies the meta-data specifications for the action that's specified in various Intent parameters. For more information, see the class about Interacting with Other Apps.

Note: The reference to DisplayMessageActivity will raise an error if you’re using an IDE such as Eclipse because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon.

An intent not only allows you to start another activity, but it can carry a bundle of data to the activity as well. So, use findViewById() to get the EditText element and add its text value to the intent:

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

EditText editText = (EditText) findViewById(;

String message = editText.getText().toString();

intent.putExtra(EXTRA_MESSAGE, message);

An Intent can carry a collection of various data types as key-value pairs called extras. The putExtra() method takes the key name in the first parameter and the value in the second parameter.


  • Oculus公布原型机,大幅度提升可视角,能实现140°的视场水平


  • teamLab创始人猪子寿之: 抛去衣食住行,我还剩下什么?

    teamLab创始人猪子寿之: 抛去衣食住行,我还剩下什么?

  • Michael I. Jordan带你解读百万奖金ATEC蚂蚁人工智能大赛

    Michael I. Jordan带你解读百万奖金ATEC蚂蚁人工智能大赛

  • 2018深圳国际人工智能展览会 2018 shenzhen International Artif

    2018深圳国际人工智能展览会 2018 shenzhen International Artif



近日,美国软性机器抓手制造商 Soft Robotics 宣布,获得 2000 万美元的融资,本轮投资者包括 Scale Venture Partners,Calibrate Ventures...

据外媒报道,加州车管局发布了《2017自动驾驶脱离报告(California Autonomous Vehicle Disengagement Reports)》,其中谈及了脱离的具体...