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