[Android开发学习20]多个Activity间的交互之Intent
一、基础知识:
1.一个Intent对象包含了一组信息:
1. Component name 指定启动的Activity
2. Action 要做什么
3. Data 传送数据
4. Category
5. Extras 键值对
6. Flags
2.Intent基本用法:
[java] view plaincopyprint?// 生成一个Intent对象
Intent intent = new Intent();
intent.putExtra("testIntent", "123"); // 传递数据
intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一个Activity(第二个参数)
//Activity_02.this.startActivity(intent);
startActivity(intent);
// 生成一个Intent对象
Intent intent = new Intent();
intent.putExtra("testIntent", "123"); // 传递数据
intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一个Activity(第二个参数)
//Activity_02.this.startActivity(intent);
startActivity(intent);
[java]
// 接收Intent传过来的数据
Intent intent = getIntent();
String value = intent.getStringExtra("testIntent"); // 接收Intent的数据
myTextView = (TextView)findViewById(R.id.myTextView);
//myTextView.setText(R.string.other);
myTextView.setText(value);
// 接收Intent传过来的数据
Intent intent = getIntent();
String value = intent.getStringExtra("testIntent"); // 接收Intent的数据
myTextView = (TextView)findViewById(R.id.myTextView);
//myTextView.setText(R.string.other);
myTextView.setText(value);
3.按钮事件的注册:
[java]
private Button myButton = null;
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 生成一个Intent对象
Intent intent = new Intent();
intent.putExtra("testIntent", "123"); // 传递数据
intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一个Activity(第二个参
数)
//Activity_02.this.startActivity(intent);
startActivity(intent);
/*
Uri uri = Uri.parse("smsto:0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
*/
}
}
private Button myButton = null;
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 生成一个Intent对象
Intent intent = new Intent();
intent.putExtra("testIntent", "123"); // 传递数据
intent.setClass(Activity_02.this, OtherActivity.class); // 指定跳向哪一个Activity(第二个参
数)
//Activity_02.this.startActivity(intent);
startActivity(intent);
/*
Uri uri = Uri.parse("smsto:0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
*/
}
}
二、代码展示:
1."Activity_02\src\yan\activity_02\Activity_02.java"
[java]
package yan.activity_02;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity_02 extends Activity {
private Button myButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_02);
myButton = (Button)findViewById(R.id.myButton);
myButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 生成一个Intent对象
Intent intent = new Intent();
intent.putExtra("testIntent", "123");
intent.setClass(Activity_02.this, OtherActivity.class);
//Activity_02.this.startActivity(intent);
startActivity(intent);
/*
Uri uri = Uri.parse("smsto:0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
*/
}
}
}
package yan.activity_02;
import android.net.Uri;
import android.os.Bu
补充:移动开发 , Android ,