Android Intent数据传输


// 目录结构


//运行结果


//SimpleDemo01Activity.java

  1. package sn.len.simple;  
  2.   
  3. import Android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9.   
  10. public class SimpleDemo01Activity extends Activity implements OnClickListener   
  11. {  
  12.   
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState)   
  15.     {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         View openOtherAct=findViewById(R.id.openotheractivity);  
  19.         openOtherAct.setOnClickListener(this);  
  20.           
  21.     }  
  22.   
  23.     @Override  
  24.     public void onClick(View v)  
  25.     {  
  26.         switch (v.getId())  
  27.         {  
  28.         case R.id.openotheractivity:  
  29.               
  30.             Intent intent=new Intent(this,OtherActivity.class);  
  31.             //传参写法1   
  32.             intent.putExtra("name""杨炼");  
  33.             intent.putExtra("age"18);  
  34.             //传参写法2   
  35.             Bundle bundle=new Bundle();  
  36.             bundle.putString("name""xiaoyang");  
  37.             bundle.putInt("age"18);  
  38.             intent.putExtras(bundle);  
  39.             //激活另一个Activity   
  40.             //startActivity(intent);   
  41.             startActivityForResult(intent, 100);  
  42.             break;  
  43.         default:  
  44.             break;  
  45.         }  
  46.     }  
  47.     @Override  
  48.     protected void onActivityResult(int requestCode, int resultCode, Intent data)   
  49.     {  
  50.         if(resultCode==20//判断其它关闭   
  51.         {  
  52.             String str=data.getExtras().getString("close");  
  53.             Log.i("CLOSE", str);  
  54.         }  
  55.         else  
  56.         {  
  57.             Log.i("CLOSE""叫你不按按钮关闭,如果不判断响应代码就出异常了吧。");  
  58.         }  
  59.           
  60.     }  
  61.       
  62. }  
//OtherActivity.java
  1. package sn.len.simple;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11. public class OtherActivity extends Activity implements OnClickListener  
  12. {  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState)   
  16.     {  
  17.         // TODO Auto-generated method stub   
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.other);  
  20.         Intent intent=this.getIntent();  
  21.         //取得方法1   
  22.         String name1=intent.getStringExtra("name");  
  23.         int age1=intent.getIntExtra("age",0);  
  24.         //取得方法2   
  25.         String name2=intent.getExtras().getString("name");  
  26.         int age2=intent.getExtras().getInt("age");  
  27.         TextView textView1=(TextView)findViewById(R.id.simplepro1);  
  28.         TextView textView2=(TextView)findViewById(R.id.simplepro2);  
  29.         textView1.setText("第一种:name"+name1+"    age"+age1);  
  30.         textView2.setText("第二种:name"+name2+"    age"+age2);  
  31.           
  32.           
  33.           
  34.         Button close=(Button)findViewById(R.id.close);  
  35.         close.setOnClickListener(this);  
  36.           
  37.     }  
  38.   
  39.     @Override  
  40.     public void onClick(View v)   
  41.     {  
  42.         switch(v.getId())  
  43.         {  
  44.             case R.id.close:  
  45.             Intent intent2=new Intent();  
  46.             intent2.putExtra("close""已经关掉了");  
  47.             setResult(20, intent2);  
  48.             finish();  
  49.             break;  
  50.         }  
  51.           
  52.     }  
  53.       
  54.       
  55. }  

相关内容