读书人

[Andriod官方锻炼教程]创建你的第一个

发布时间: 2013-03-06 16:20:31 作者: rapoo

[Andriod官方训练教程]创建你的第一个App之开始另一个Activity

原文地址:http://developer.android.com/training/basics/firstapp/starting-activity.html

------------------------------------------------------

After completing the previous lesson, you have an app that shows an activity (a single screen) with a text field and a button. In this lesson, you’ll add some code to MainActivity that starts a new activity when the user clicks the Send button.

在完成上一节课后,你已经有了一个显示一个activity(单独一个屏幕)的app,它包含一个文本框和一个按键。在这节课里,你将会向MainActivity添加一些代码,使得用户点击Send按键后开始一个新的activity。

Respond to the Send Button —— 响应发送按键

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

为了响应按键的点击事件,打开 activity_main.xml 布局文件,并向 <Button> 元素添加 android:onClick 属性。

<Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="@string/button_send"    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.

android:onClick 属性的值,"sendMessage",是你的activity中的一个方法名字,当用户点击该按键时系统将调用它。

Open the MainActivity class (located in the project's src/ directory) and add the corresponding method:

打开 MainActivity 类(放在项目的 src/ 目录下),然后添加对应的方法:

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

This requires that you import the View class:

这要求你导入 View 类:

import android.view.View;

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

提示:在Eclipse里,按 Ctrl + Shift + O 来导入缺少的类(Mac里是Cmd + Shift + O)。

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:

为了让系统将该方法与android:onClick中给出的方法名称相匹配,标签必须像上面展示的那样精确。特别的,该方法必须:

读书人网 >移动开发

热点推荐