android开发中是用Intent从一个activity跳向一个activity的问题
2016-03-06 14:44
441 查看
安卓中经常会使用页面跳转的问题,在安卓中主要有需要返回参数和不需要返回参数的两种方式,提醒大家在写了activity以后,要记得在AndroidManifest中声明。
代码注释如下:
第一个界面
public class MainActivity extends Activity {
// 定义按钮,文本框
private Button button;
private Button button1;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 通过fbutton实现页面之间的跳转
*/
button = (Button) findViewById(R.id.fbutton);
tv = (TextView) findViewById(R.id.textView1);
// 注册点击事件
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 初始化Intent,第一个参数,上下文对象this,第二个参数目标文件
Intent intent = new Intent(MainActivity.this, Factivity.class);
// startActivity的方式来实现
startActivity(intent);
}
});
button1 = (Button) findViewById(R.id.sbutton);
// 为button1设置监听事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Factivity.class);
// 第一个参数为intent和没有返回值得startActivity差不多,第二个参数是RequestCode,也就是请求码
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// 结果码为另一个activity里面setResult方法实现
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
String s = data.getStringExtra("data");
tv.setText(s);
}
}
}
第二个界面
public class Factivity extends Activity{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data =new Intent();
//putExtra和数据结构中的map一样,前面为键值,后面为对应的值
data.putExtra("data", "hello");
//setResult第一个参数为结果码,data本质为一个intent
setResult(2, data);
//关闭当前页面
finish();
}
});
}
}
第一个界面的xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/fbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="第一种启动方式" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fbutton"
android:layout_centerVertical="true"
android:text="记录activity返回的值" />
<Button
android:id="@+id/sbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fbutton"
android:layout_below="@+id/fbutton"
android:layout_marginTop="34dp"
android:text="第二种启动方式" />
</RelativeLayout>
第二个界面的xml的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一种启动方式" />
</LinearLayout>
代码注释如下:
第一个界面
public class MainActivity extends Activity {
// 定义按钮,文本框
private Button button;
private Button button1;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* 通过fbutton实现页面之间的跳转
*/
button = (Button) findViewById(R.id.fbutton);
tv = (TextView) findViewById(R.id.textView1);
// 注册点击事件
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 初始化Intent,第一个参数,上下文对象this,第二个参数目标文件
Intent intent = new Intent(MainActivity.this, Factivity.class);
// startActivity的方式来实现
startActivity(intent);
}
});
button1 = (Button) findViewById(R.id.sbutton);
// 为button1设置监听事件
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Factivity.class);
// 第一个参数为intent和没有返回值得startActivity差不多,第二个参数是RequestCode,也就是请求码
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
// 结果码为另一个activity里面setResult方法实现
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
String s = data.getStringExtra("data");
tv.setText(s);
}
}
}
第二个界面
public class Factivity extends Activity{
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent data =new Intent();
//putExtra和数据结构中的map一样,前面为键值,后面为对应的值
data.putExtra("data", "hello");
//setResult第一个参数为结果码,data本质为一个intent
setResult(2, data);
//关闭当前页面
finish();
}
});
}
}
第一个界面的xml布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/fbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="第一种启动方式" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fbutton"
android:layout_centerVertical="true"
android:text="记录activity返回的值" />
<Button
android:id="@+id/sbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/fbutton"
android:layout_below="@+id/fbutton"
android:layout_marginTop="34dp"
android:text="第二种启动方式" />
</RelativeLayout>
第二个界面的xml的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一种启动方式" />
</LinearLayout>
相关文章推荐
- Android Studio中mergeDebugResources时报“png-cruncher_*”异常死机的解决方案
- Android中Canvas画图
- Android入门开发报错通用自查列表
- Android四大组件之一广播
- android应用使用情况统计实现
- Android 6.0 RunTimePermission(运行时授权)
- Android开发问题汇总–持续更新
- Android常用开发工具、站点集合
- Android控件第1类——TextView
- android 沉浸式导航栏的使用
- Android 安全专项-Xposed 劫持用户名密码实践
- Android界面性能调优手册
- Android开发之ContentProvider与ContentResolver的使用
- <android编译ffmpeg+x264+faac> <Android/NDK环境下FFmpeg及AAC,MP3,X264的编译>
- Android Mediaplayer 三种从资源文件中获取数据播放
- 动态输入日期与时间(DatePicker与TimePicker应用)
- 第4章 Android odex文件格式 第四节
- android--List属性
- Android 常用代码
- [android] 采用断点调试的方式观察pull解析的流程