android,两个活动页面之间的跳转

  1. public class Intent1Activity extends Activity {  
  2. private Button btn = null;  
  3. private TextView tv = null;  
  4. //还是用个变量比较好,原因在于你这个页面可能有两个button都是转到别的页面  
  5. //回调的时候你必须要根据这个来判断的,直接写数字别人不易理解  
  6. private final int REQUEST_CODE = 1;  
  7. @Override  
  8. protected void onCreate(Bundle savedInstanceState) {  
  9. super.onCreate(savedInstanceState);  
  10. setContentView(R.layout.intent1);  
  11. tv=(TextView)findViewById(R.id.intent1textview);  
  12. btn = (Button)findViewById(R.id.intent1button);  
  13. //设置单击事件  
  14. btn.setOnClickListener(new OnClickListener() {  
  15.         @Override  
  16.         public void onClick(View v) {  
  17.            Intent intent = new Intent();  
  18.               //intent.setClass(Context context, Class<?> clazz);  
  19. //第一个参数指的就是要跳转的那个Activity  
  20. //第二个指的是跳到的那个Activity  
  21.         intent.setClass(Intent1Activity.this,   
  22. Intent2Activity.class);  
  23.         intent.putExtra("str1""hello...");  
  24.         intent.putExtra("str2""world...");  
  25.         //如果要传一个对象呢?intent.pputExtra(String name,   
  26. Serializable value);  
  27.         //startActivity(intent);//这个是如果没有回调的时候直接这样就好  
  28.         //如果我们希望下一个Activity回传参数咋办呢?上面的就不行了  
  29.         startActivityForResult(intent, REQUEST_CODE);  
  30.        }  
  31. });  
  32. }  
  33. //如果没有回调也不用重写这个方法  
  34. @Override  
  35. protected void onActivityResult(int requestCode,  
  36.            int resultCode, Intent data) {  
  37. //第一个指的是这个页面的requestcode  
  38. //第二个是Intent2Activity里面的requestcode  
  39. if(requestCode != REQUEST_CODE){  
  40. tv.setText("wrong");  
  41. }  
  42. if(resultCode != new Intent2Activity().REQUEST_CODE){  
  43. tv.setText("wrong");  
  44. }  
  45. String result = data.getExtras().getString("str3");  
  46. tv.setText(result);  
  47. }  
  48. }  
  49. public class Intent2Activity extends Activity {  
  50. private Button btn = null;  
  51. private TextView tv = null;  
  52. protected final int REQUEST_CODE = 1;  
  53. @Override  
  54. protected void onCreate(Bundle savedInstanceState) {  
  55. super.onCreate(savedInstanceState);  
  56.      setContentView(R.layout.intent2);  
  57.      tv = (TextView)findViewById(R.id.intent2textview);  
  58.      btn = (Button)findViewById(R.id.intent2button);  
  59.      Intent intent = this.getIntent();  
  60.      Bundle bundle = intent.getExtras();  
  61.      String str1 = bundle.getString("str1");  
  62.      String str2 = bundle.getString("str2");  
  63.           
  64.      tv.setText(str1+"\t"+str2);  
  65.      btn.setOnClickListener(new OnClickListener() {  
  66.          @Override  
  67.          public void onClick(View v) {  
  68.              Intent intent = new Intent();  
  69.              intent.putExtra("str3""Back Data");  
  70.              setResult(REQUEST_CODE, intent);  
  71.              finish();  
  72. //调用这个方法,那么此Activity关闭,并把它从栈中移除。  
  73. //然后进入到前一个Activity  
  74.     }  
  75. });  
  76. }  
  77. }  

<li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;Intent1Activity&nbsp;</span><span class="keyword">extends</span><span>&nbsp;Activity&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">private</span><span>&nbsp;Button&nbsp;btn&nbsp;=&nbsp;</span><span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">private</span><span>&nbsp;TextView&nbsp;tv&nbsp;=&nbsp;</span><span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="comment">//还是用个变量比较好,原因在于你这个页面可能有两个button都是转到别的页面</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="comment">//回调的时候你必须要根据这个来判断的,直接写数字别人不易理解</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">private</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;</span><span class="keyword">int</span><span>&nbsp;REQUEST_CODE&nbsp;=&nbsp;</span><span class="number">1</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onCreate(Bundle&nbsp;savedInstanceState)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">super</span><span>.onCreate(savedInstanceState);&nbsp;&nbsp;</span></span></li><li class=""><span>setContentView(R.layout.intent1);&nbsp;&nbsp;</span></li><li class="alt"><span>tv=(TextView)findViewById(R.id.intent1textview);&nbsp;&nbsp;</span></li><li class=""><span>btn&nbsp;=&nbsp;(Button)findViewById(R.id.intent1button);&nbsp;&nbsp;</span></li><li class="alt"><span><span class="comment">//设置单击事件</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>btn.setOnClickListener(<span class="keyword">new</span><span>&nbsp;OnClickListener()&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onClick(View&nbsp;v)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Intent&nbsp;intent&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Intent();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//intent.setClass(Context&nbsp;context,&nbsp;Class<?>&nbsp;clazz);</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="comment">//第一个参数指的就是要跳转的那个Activity</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="comment">//第二个指的是跳到的那个Activity</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intent.setClass(Intent1Activity.<span class="keyword">this</span><span>,&nbsp;&nbsp;&nbsp;</span></span></li><li class=""><span>Intent2Activity.<span class="keyword">class</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intent.putExtra(<span class="string">"str1"</span><span>,&nbsp;</span><span class="string">"hello..."</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intent.putExtra(<span class="string">"str2"</span><span>,&nbsp;</span><span class="string">"world..."</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//如果要传一个对象呢?intent.pputExtra(String&nbsp;name,&nbsp;</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>Serializable&nbsp;value);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//startActivity(intent);//这个是如果没有回调的时候直接这样就好</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">//如果我们希望下一个Activity回传参数咋办呢?上面的就不行了</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;startActivityForResult(intent,&nbsp;REQUEST_CODE);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>});&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span><span class="comment">//如果没有回调也不用重写这个方法</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onActivityResult(</span><span class="keyword">int</span><span>&nbsp;requestCode,&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">int</span><span>&nbsp;resultCode,&nbsp;Intent&nbsp;data)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="comment">//第一个指的是这个页面的requestcode</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="comment">//第二个是Intent2Activity里面的requestcode</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">if</span><span>(requestCode&nbsp;!=&nbsp;REQUEST_CODE){&nbsp;&nbsp;</span></span></li><li class=""><span>tv.setText(<span class="string">"wrong"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span><span class="keyword">if</span><span>(resultCode&nbsp;!=&nbsp;</span><span class="keyword">new</span><span>&nbsp;Intent2Activity().REQUEST_CODE){&nbsp;&nbsp;</span></span></li><li class="alt"><span>tv.setText(<span class="string">"wrong"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>String&nbsp;result&nbsp;=&nbsp;data.getExtras().getString(<span class="string">"str3"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>tv.setText(result);&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span><span class="keyword">public</span><span>&nbsp;</span><span class="keyword">class</span><span>&nbsp;Intent2Activity&nbsp;</span><span class="keyword">extends</span><span>&nbsp;Activity&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">private</span><span>&nbsp;Button&nbsp;btn&nbsp;=&nbsp;</span><span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">private</span><span>&nbsp;TextView&nbsp;tv&nbsp;=&nbsp;</span><span class="keyword">null</span><span>;&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">final</span><span>&nbsp;</span><span class="keyword">int</span><span>&nbsp;REQUEST_CODE&nbsp;=&nbsp;</span><span class="number">1</span><span>;&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span><span class="keyword">protected</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onCreate(Bundle&nbsp;savedInstanceState)&nbsp;{&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="keyword">super</span><span>.onCreate(savedInstanceState);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setContentView(R.layout.intent2);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv&nbsp;=&nbsp;(TextView)findViewById(R.id.intent2textview);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btn&nbsp;=&nbsp;(Button)findViewById(R.id.intent2button);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Intent&nbsp;intent&nbsp;=&nbsp;<span class="keyword">this</span><span>.getIntent();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Bundle&nbsp;bundle&nbsp;=&nbsp;intent.getExtras();&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str1&nbsp;=&nbsp;bundle.getString(<span class="string">"str1"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;str2&nbsp;=&nbsp;bundle.getString(<span class="string">"str2"</span><span>);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tv.setText(str1+<span class="string">"\t"</span><span>+str2);&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;btn.setOnClickListener(<span class="keyword">new</span><span>&nbsp;OnClickListener()&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="annotation">@Override</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">public</span><span>&nbsp;</span><span class="keyword">void</span><span>&nbsp;onClick(View&nbsp;v)&nbsp;{&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Intent&nbsp;intent&nbsp;=&nbsp;<span class="keyword">new</span><span>&nbsp;Intent();&nbsp;&nbsp;</span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intent.putExtra(<span class="string">"str3"</span><span>,&nbsp;</span><span class="string">"Back&nbsp;Data"</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setResult(REQUEST_CODE,&nbsp;intent);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finish();&nbsp;&nbsp;</span></li><li class=""><span><span class="comment">//调用这个方法,那么此Activity关闭,并把它从栈中移除。</span><span>&nbsp;&nbsp;</span></span></li><li class="alt"><span><span class="comment">//然后进入到前一个Activity</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>});&nbsp;&nbsp;</span></li><li class=""><span>}&nbsp;&nbsp;</span></li><li class="alt"><span>}&nbsp;&nbsp;</span></li>

posted @ 2016-03-14 08:39  拾荒者999  阅读(2586)  评论(0编辑  收藏  举报