原文地址:http://developer.android.com/training/basics/firstapp/building-ui.html
响应按钮
- 在Android Studio, 从
res/layout目录, 编辑activity_my.xml文件. - 在
<Button>元素, 添加android:onClick属性.res/layout/activity_my.xml
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android: />android:onClick属性的值,"sendMessage", 是用户点击按钮后调用的函数. - 在
java/com.mycompany.myfirstapp目录, 打开MyActivity.java文件. - 在
MyActivity类, 添加sendMessage()方法.java/com.mycompany.myfirstapp/MyActivity.java
/** Called when the user clicks the Send button */ public void sendMessage(View view) { // Do something in response to button }为了匹配
android:onClick指定的方法,定义的方法必须是:
创建一个 Intent
- 在
MyActivity.java,sendMessage()方法, 创建Intent启动名为DisplayMessageActivity的Activity:java/com.mycompany.myfirstapp/MyActivity.java
public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); }Intents
An
Intentis an object that provides runtime binding between separate components (such as two activities). TheIntentrepresents 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. For more information, see Intents and Intent Filters.构造函数获得两个参数:
Android Studio 提示,必须导入
Intent类. - 文件顶端,导入
Intentclass:java/com.mycompany.myfirstapp/MyActivity.java
import android.content.Intent;
小技巧: 在 Android Studio, 按 Alt + Enter (option + return on Mac) 导入缺少的 classes.
- Inside the
sendMessage()method, usefindViewById()to get theEditTextelement.java/com.mycompany.myfirstapp/MyActivity.java
public void sendMessage(View view) { Intent intent = new Intent(this, DisplayMessageActivity.class); EditText editText = (EditText) findViewById(R.id.edit_message); } - 导入
EditTextclass. - 文本赋值给本地变量
message, 使用putExtra()将值添加到 intent.java/com.mycompany.myfirstapp/MyActivity.java
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可携带 key-value 对,称为 extras.putExtra()用来添加key-value对. - 在
MyActivity顶部, 定义EXTRA_MESSAGE:java/com.mycompany.myfirstapp/MyActivity.java
public class MyActivity extends AppCompatActivity { public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE"; ... }
- 在
sendMessage()方法, 调用startActivity()方法
完整的 sendMessage() 方法如下:
java/com.mycompany.myfirstapp/MyActivity.java
/** 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);
}
创建第二个 Activity
所有 Activity 的子类必须实现 onCreate() 方法. Activity在此方法中接收Intent的信息. 同时, onCreate() 必须调用setContentView() 方法设置布局.
Android Studio中创建Activity
Figure 1. The new activity wizard in Android Studio.
- 在Android Studio, 在
java目录, 选择包, com.mycompany.myfirstapp, 右键,选择 New > Activity > Blank Activity. - 在 Choose options 窗口, 填写如下:
- Activity Name: DisplayMessageActivity
- Layout Name: activity_display_message
- Title: My Message
- Hierarchical Parent: com.mycompany.myfirstapp.MyActivity
- Package name: com.mycompany.myfirstapp
Click Finish.
- 打开
DisplayMessageActivity.java文件. - 移除
onCreateOptionsMenu()方法.
接收Intent
每个 Activity 由一个 Intent调用, 无论用户如何导向到那个Activity. 调研 getIntent() 可获取调用了本Activity的 intent,从中获取信息.
- 在
java/com.mycompany.myfirstapp目录, 编辑DisplayMessageActivity.java文件. - 在
onCreate()方法, 移除:setContentView(R.layout.activity_display_message);
- 获取intent.
Intent intent = getIntent();
- 文件头部,导入
Intent. - 使用
getStringExtra()方法获取传递过来的信息.String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
显示信息
- 在
onCreate()方法, 创建TextView对象.TextView textView = new TextView(this);
- 设置尺寸和文本.
textView.setTextSize(40); textView.setText(message);
- 将
TextView设为根view.setContentView(textView);
- 文件顶部,导入
TextView类.
完整的 onCreate() 方法如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
运行app。
Figure 2. Both activities in the final app, running on Android 4.4.
就这样,创建了第一个 Android app!

本文介绍如何在Android应用中实现按钮响应及通过Intent在不同Activity间传递数据。
8656

被折叠的 条评论
为什么被折叠?



