用Intent实现activity的跳转

新建一个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中注册

上一篇:python实现并行爬虫


下一篇:百度AIG知识图谱部算法实习生面经(已拿offer)