使用Intent实现页面跳转 (另一种方法)
2016-11-11 16:14
302 查看
package com.example.demo1; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class FActivity extends Activity { private Button bt1; private Button bt2; private Context mContext; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.factivity); /* * 通过点击bt1实现页面之间的跳转 * 1.startActivity的方式来实现 * <1>初始化Intent */ mContext=this; tv=(TextView) findViewById(R.id.textView1); bt1=(Button)findViewById(R.id.button1_first); bt2=(Button) findViewById(R.id.button2_second); //注册点击事件 bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /* * 第一个参数:上下文对象this * 第二个参数:目标文件 */ Intent intent=new Intent(mContext, SActivity.class); startActivity(intent); } }); /* * 通过startActivityForresult */ bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent=new Intent(mContext, SActivity.class); /* * 第一个参数是Intent对象 * 第二个参数是请求的一个标识 */ startActivityForResult(intent, 1); } }); } /* * 通过startActivityForresult跳转,接受返回数据的方法 * requestCode:请求的标识 * resultCode:代表第二个页面返回的标识 * data:第二个页面回传的数据 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==1&&resultCode==2){ String content=data.getStringExtra("data"); tv.setText(content); } } }
package com.example.demo1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SActivity extends Activity{
private Button bt;
private String content="你好";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
/*
* 第二个页面什么时候给第一个页面回传数据
* 回传到第一个页面的实际上是一个Intent对象
*/
bt=(Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent data=new Intent();
data.putExtra("data", content);
setResult(2, data);
//结束当前页面
finish();
}
});
}
}
相关文章推荐
- Android Activity中使用Intent实现页面跳转与参数传递的方法
- 使用Intent实现页面跳转
- Android编程学习笔记 之 使用Intent实现页面跳转
- Android Intent实现页面跳转的两种方法
- Android开发 第13课 使用Intent实现页面跳转
- Android Studio 使用Intent实现页面的跳转(带参数)
- Android成长日记-使用Intent实现页面跳转
- 使用js实现页面跳转方法
- Android使用Intent实现页面跳转
- Activity中使用Intent实现页面跳转与参数的传递
- 使用Javascript实现跳转页面和打开新窗口的方法
- asp.net中倒计时自动跳转页面的实现方法(使用javascript)
- Activity中使用Intent实现页面跳转与参数的传递
- 使用 present 方法实现页面返回跳转(多层返回跳转)
- Android使用Intent隐式实现页面跳转
- 使用Intent实现页面跳转
- 使用Intent实现页面跳转
- Android使用Intent实现页面跳转
- Android入门(8)——使用Intent实现页面跳转
- android开发基础:Intent与Bundle的使用,实现页面跳转及信息传递