Activity中使用Intent实现页面跳转与参数的传递

新建一个FirstAvtivity.java

package com.zhuguangwei;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class FirstActivity extends Activity {
private Button myButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myButton
= (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(
new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
//Intent传递参数
intent.putExtra("testIntent", "123");
intent.setClass(FirstActivity.
this, SecondActivity.class);
FirstActivity.
this.startActivity(intent);
}
});
}
}
新建第二个SecondActivity.java

package com.zhuguangwei;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity{
private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
//使用Intent对象得到FirstActivity传递来的参数
Intent intent = getIntent();
String value
= intent.getStringExtra("testIntent");

myTextView
= (TextView) findViewById(R.id.myTextView);
myTextView.setText(value);
}
}

两个Activity都要在AndroidMenifest.xml中注册

posted @ 2011-01-25 10:47  无忧一生  阅读(23591)  评论(2编辑  收藏  举报