Android学习笔记(二)——使用Intent传数据之通用方式

1、Intent(意图):Intent是一种运行时绑定机制(runtime binding),用于在两个Activity之间传递数据。Intent也具有媒体中介的作用,实现调用者与被调用者之间的解耦;

2、Intent传递数据通用方式:(1)在Main.java中构造一个Intent;(2)然后调用startActivity(intent)将构造的Intent传入;(3)系统根据Intent中的描述,到Manifest.xml寻找满足此要求的Activity(4)系统会调用这个Activity,传入Intent,执行相应的操作~

3、新建Android项目”android_intent“,创建Activity名为”Main“;

4、在“res/layout/main.xml”中添加一个”Button“,完成布局文件的编写,代码如下:

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="测试使用Intent传递数据" >
    </Button>

5、新建“OtherActivity.java”,并在“Superclass”中选择“android.app.Activity”(继承Activity),并在其中添加onCreate()方法(代码在步骤9);

6、接着声明一个布局文件,在“res/layout”中新建“other.xml”,将其代码修改如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/msg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </TextView>

</LinearLayout>

7、打开“src”中的“Main.java”,在Main类中添加代码以构造Intent并启动Intent,添加后代码如下:

public class Main extends Activity {
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);// 加载布局文件
		button = (Button) this.findViewById(R.id.button);//根据Id查找视图
		button.setOnClickListener(new View.OnClickListener() {//点击,启动点击事件,在其中新建意图

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				//由Main.this传入OtherActivity.class,表示从当前意图传入目标意图。
				Intent intent = new Intent(Main.this, OtherActivity.class);
				// 在意图中传递数据,将数据临时存储在内存中
				intent.putExtra("name", "张三");
				intent.putExtra("age", 23);
				intent.putExtra("address", "北京");
				// 启动意图
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
8、打开“AndroidManifest.xml”,在“Application”标签下添加一个“Activity”标签(以使系统在传入Intent后能查找到名为“OtherActivity”的活动),代码如下:

        <activity android:name=".OtherActivity" >//用“.”表示当前这个类
        </activity>
9、在“OtherActivity.java”中添加一个“getIntent”方法,获取意图,随后调用get方法获取数据,再将获取的信息放入“TextView”中。完整代码如下:

public class OtherActivity extends Activity {
	private TextView textview;

	public OtherActivity() {
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {//创建Activity
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);// 加载布局文件(必须!)
		textview = (TextView) this.findViewById(R.id.msg);
		//使用getIntent方法获取意图
		Intent intent = getIntent();
		//使用get方法取出Intent中的数据
		int age = intent.getIntExtra("age", 0);
		String name = intent.getStringExtra("name");
		String address = intent.getStringExtra("address");
		textview.setText("age--->>" + age + "\n" + "name--->>" + name + "\n"
				+ "address--->>" + address + "\n");
	}
}
10、右击“src”下的包,选择“Run as”中的“Android Application”来运行程序,结果截图如下:


点击“Button”显示:


Ps:第一个Android程序,见笑~

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值