雾山的Android学习笔记---Intent

在android平台中,Activity组件之间的交互通过意向(Intent)来实现。

Intent是android的组件之一(激活组件,用于激活其他组件),Intent对象在android系统中代表一种意图,Intent中最重要的内容是action与data

使用Intent对象传递数据的步骤:

1)在Activity之间可以使用Intent对象传递数据

2)使用putExtra()系列方法向Intent对象当中储存数据

3)使用getXXXExtra()系列方法从Intent对象当中取出数据

下面是代码示例:

主Activity:

  1. package com.tangbc.s02e04_intent;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.Menu;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.     private Button button;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.           
  19.         button = (Button)findViewById(R.id.button);  
  20.         button.setOnClickListener(new ButtonListener());  
  21.     }  
  22.       
  23.     class ButtonListener implements OnClickListener{  
  24.         @Override  
  25.         public void onClick(View arg0) {  
  26.             //生成一个Intent对象   
  27.             Intent intent = new Intent();  
  28.             //调用Intent对象的setClass()方法   
  29.             //setClass(Context packageContext, Class<?> cls)   
  30.             intent.setClass(MainActivity.this, Other.class);  
  31.             //在该Intent对象中存入数据   
  32.             intent.putExtra("com.tangbc.s02e04_intent.Age"20);  
  33.             intent.putExtra("com.tangbc.s02e04_intent.Name","tang");  
  34.             //启动第二个Activity   
  35.             startActivity(intent);  
  36.         }  
  37.     }  
  38. }  
package com.tangbc.s02e04_intent;

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

public class MainActivity extends Activity {
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new ButtonListener());
	}
	
	class ButtonListener implements OnClickListener{
		@Override
		public void onClick(View arg0) {
			//生成一个Intent对象
			Intent intent = new Intent();
			//调用Intent对象的setClass()方法
			//setClass(Context packageContext, Class<?> cls)
			intent.setClass(MainActivity.this, Other.class);
			//在该Intent对象中存入数据
			intent.putExtra("com.tangbc.s02e04_intent.Age", 20);
			intent.putExtra("com.tangbc.s02e04_intent.Name","tang");
			//启动第二个Activity
			startActivity(intent);
		}
	}
}

第二个Activity:

  1. package com.tangbc.s02e04_intent;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class Other extends Activity{  
  9.     private TextView textView;  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.other);  
  15.         //将返回启动这个Activity的Intent对象赋给intent   
  16.         Intent intent = getIntent();  
  17.         //将intent中的数据取出并赋值   
  18.         //括号中第二个数是默认值,若没有对应的值,就取默认值   
  19.         int age = intent.getIntExtra("com.tangbc.s02e04_intent.Age"10);  
  20.         int age1 = intent.getIntExtra("com.tangbc.s02e04_intent.Age1"10);  
  21.         String name = intent.getStringExtra("com.tangbc.s02e04_intent.Name");  
  22.           
  23.         textView = (TextView)findViewById(R.id.textView);  
  24.         textView.setText("name= " + name + ",age= " + age + ",age1=" + age1);  
  25.     }  
  26. }  
package com.tangbc.s02e04_intent;

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

public class Other extends Activity{
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		//将返回启动这个Activity的Intent对象赋给intent
		Intent intent = getIntent();
		//将intent中的数据取出并赋值
		//括号中第二个数是默认值,若没有对应的值,就取默认值
		int age = intent.getIntExtra("com.tangbc.s02e04_intent.Age", 10);
		int age1 = intent.getIntExtra("com.tangbc.s02e04_intent.Age1", 10);
		String name = intent.getStringExtra("com.tangbc.s02e04_intent.Name");
		
		textView = (TextView)findViewById(R.id.textView);
		textView.setText("name= " + name + ",age= " + age + ",age1=" + age1);
	}
}

主Activity的XML代码:

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <Button   
  12.         android:id="@+id/button"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="touch me"  
  16.         />  
  17.   
  18. </RelativeLayout>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="touch me"
        />

</RelativeLayout>

副Activity的XML代码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView   
  8.         android:id="@+id/textView"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content" />  
  11.       
  12. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
</LinearLayout>

最后的结果图:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值