转自:http://www.cnblogs.com/tdalcn/p/3467445.html
MainActivity
- package com.test.intentdemo;
- import android.app.Activity;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends Activity {
- private Button btn=null;
- public void onCreate(Bundle savedInstanceState)//onCreate方法用来初始化Activity实例对象
- {
- super.onCreate(savedInstanceState);//super.onCreate(savedInstanceState)的作用是调用其父类Activity的onCreate方法来实现对界面的图画绘制工作
- setContentView(R.layout.activity_main);//setContentView(R.layout.main)的作用是加载一个界面
- btn=(Button)findViewById(R.id.btn);
- btn.setOnClickListener(listener);
- }
- private View.OnClickListener listener=new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- // Intent intent=new Intent();
- // intent.setAction(Intent.ACTION_SENDTO);
- // intent.setData(Uri.parse("smsto:5554"));
- // intent.putExtra("sms_body", "Hello!");//sms_body不能随意换成
- // startActivity(intent);
- Intent intent=new Intent();
- intent.setClass(MainActivity.this, SecondActivity.class);//从一个activity跳转到另一个activity
- intent.putExtra("str", "Intent Demo");//给intent添加额外数据,key为“str”,key值为"Intent Demo"
- startActivity(intent);
- }
- };
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
secondAcitivity
- package com.test.intentdemo;
- import android.R.string;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
- public class SecondActivity extends Activity
- {
- private TextView secondTxt;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.second);
- Intent intent=getIntent();//getIntent将该项目中包含的原始intent检索出来,将检索出来的intent赋值给一个Intent类型的变量intent
- Bundle bundle=intent.getExtras();//.getExtras()得到intent所附带的额外数据
- String str=bundle.getString("str");//getString()返回指定key的值
- secondTxt=(TextView)findViewById(R.id.secondTxt);//用TextView显示值
- secondTxt.setText(str);
- }
- }