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