1.背景:
要创建并显示一个Activity,可以调用startActivity,并传递给它一个Intent,如下面的代码所示:
startActivity(Intent myIntent);
可以构造Intent来显示地指定要打开的Activity类,或者包含一个目标Activity必须执行的动作。在后一种情况中,运行时将会使用一个称为“Intent解析(Intent resolution)”的过程来动态选择Activity。
还可以通过向Intent添加extra来向目标Activity发送额外的数据。
2.实例
利用startActivity创建并显示一个Activity,并向其传递数据。
1》程序启动时:
其布局代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.demo.parameterdemo.MainActivity" >
<EditText
android:id="@+id/info_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="在这里输入要传送的内容..." />
<Button
android:id="@+id/startAnotherActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始传递"/>
</LinearLayout>
其中“开始传递”的事件处理代码如下:
// 启动子Activity,并从中获取返回信息
private void startAnotherActivity() {
Intent startIntent = new Intent(getApplicationContext(),
AnotherActivity.class);
startIntent.putExtra(AnotherActivity.RETURN_INFO, infoEditText
.getText().toString());
/*
* //也可以使用这种方式
* Bundle argBundle=new Bundle();
* argBundle.putString(AnotherActivity.RETURN_INFO,
* infoEditText.getText().toString());
* startIntent.putExtras(argBundle);
*/
startActivity(startIntent);
}
2》输入数据,如下图所示
3》点击“开始传递”,如下图所示
可以看到已经成功,那么是如何接收到该数据呢?
private void getINFO() {
// 获取传递过来的信息。
String infoString = getIntent().getStringExtra(RETURN_INFO);
subTextView.setText(infoString);
}
3.详细代码如下:
1》MainActivity.java
package com.demo.parameterdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
// 声明控件
private EditText infoEditText;
private Button startAnotherButton;
// 请求码
private static final int ACTIVITY_REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
// 初始化
private void init() {
// 得到相应控件的引用
infoEditText = (EditText) findViewById(R.id.info_et);
startAnotherButton = (Button) findViewById(R.id.startAnotherActivity);
// 注册事件
startAnotherButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startAnotherActivity();
}
});
}
// 启动子Activity,并从中获取返回信息
private void startAnotherActivity() {
Intent startIntent = new Intent(getApplicationContext(),
AnotherActivity.class);
startIntent.putExtra(AnotherActivity.RETURN_INFO, infoEditText
.getText().toString());
/*
* //也可以使用这种方式
* Bundle argBundle=new Bundle();
* argBundle.putString(AnotherActivity.RETURN_INFO,
* infoEditText.getText().toString());
* startIntent.putExtras(argBundle);
*/
startActivity(startIntent);
}
}
2》SubActivity.java
package com.demo.parameterdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AnotherActivity extends Activity {
// 声明控件
private TextView subTextView;
// 用于唯一标识要返回的信息
// 应该尽量的长,最好加上所在类的全名,即:包名+类名+参数名
public static final String RETURN_INFO = "com.demo.parameterdemo.SubActivity.info";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
init();
getINFO();
}
// 初始化
private void init() {
// 得到相应控件引用
subTextView = (TextView) findViewById(R.id.subTextView);
}
private void getINFO() {
// 获取传递过来的信息。
String infoString = getIntent().getStringExtra(RETURN_INFO);
subTextView.setText(infoString);
}
}