Android Intent实现页面跳转的两种方法
作者:好雨天堂 时间:2022-03-13 22:27:10
本文实例为大家分享了Intent实现页面跳转的两种的方法,供大家参考,具体内容如下
下图中两个不同的方法就是两种页面之间跳转的情况
1).跳转不返回数据
2).跳转返回数据
实例:
第一种启动方式(跳转不返回数据)
第二种启动方式(跳转返回数据)
先看第一种:
点击第一种启动方式按钮会出现右边的图,然后再点击Button按钮返回左边的界面,TextView中的内容没变。
再看第二种启动方式
不同的是,点击Button按钮返回左边的界面,TextView中的内容变成了你好。
下面是所有代码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.intent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name="com.example.lenovo.intent.firstactivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.lenovo.intent.Secondactivity">
</activity>
</application>
</manifest>
factivity
<?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/bt1__first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种启动方式" />
<Button
android:id="@+id/bt2__second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种启动方式" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="吧第二个页面回传的数据显示出来" />
</LinearLayout>
sactivity
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
firstactivity.java
package com.example.lenovo.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by lenovo on 2018/2/27.
*/
public class firstactivity extends Activity {
private Button bt1;
private Button bt2;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.factivity);
/*
通过点击bt1实现界面之间的跳转
1.通过startActivity的方式来实现
1>初始Intent(意图)
*/
bt1=(Button) findViewById(R.id.bt1__first);
bt2=(Button)findViewById(R.id.bt2__second);
tv=(TextView) findViewById(R.id.textView1);
//给bt1添加点击事件
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/*
第一个参数:上下文对象this
第二个参数:目标文件
*/
Intent intent = new Intent(firstactivity.this,Secondactivity.class);
startActivity(intent);
}
});
/*
2.通过startActivityForResult的方式来实现
*/
//给bt2添加点击事件
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(firstactivity.this,Secondactivity.class);
/*
第一个参数:Intent对象
第二个参数:请求的一个标识
*/
startActivityForResult(intent,1);
}
});
}
/*
通过startActivityForResult的方式接受返回数据的方法
requestCode:请求的标志,给每个页面发出请求的标志不一样,这样以后通过这个标志接受不同的数据
resultCode:这个参数是setResult(int resultCode,Intent data)方法传来的,这个方法用在传来数据的那个页面
*/
@Override
protected void onActivityResult(int requestCode,int resultCode ,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==1&&resultCode==2){//当请求码是1&&返回码是2进行下面操作
String content=data.getStringExtra("data");
tv.setText(content);
}
}
}
Secondactivity.java
package com.example.lenovo.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by lenovo on 2018/2/27.
*/
public class Secondactivity extends Activity {
private Button bt;
String content="你好";//想返回的内容
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
/*
第二个页面什么时候给第一个页面回传数据
回传到第一个页面的实际上是一个Intent对象
*/
bt=(Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent data = new Intent();
//name相当于一个key,content是返回的内容
data.putExtra("data",content);
//resultCode是返回码,用来确定是哪个页面传来的数据,这里设置返回码是2
//这个页面传来数据,要用到下面这个方法setResult(int resultCode,Intent data)
setResult(2,data);
//结束当前页面
finish();
}
});
}
}
来源:https://blog.csdn.net/weixin_38199770/article/details/79391259
标签:Android,Intent,页面跳转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java多态成员访问的特点是什么?
2022-08-23 03:07:22
C#中的TemplateMethod模式问题分析
2021-12-01 04:02:44
![](https://img.aspxhome.com/file/2023/3/79103_0s.png)
java分割文本字符串的方法
2023-11-13 03:52:24
详解在spring boot中消息推送系统设计与实现
2023-05-28 04:17:00
![](https://img.aspxhome.com/file/2023/2/74902_0s.jpg)
SpringMVC MVC架构与Servlet使用详解
2023-10-10 15:25:02
![](https://img.aspxhome.com/file/2023/1/61301_0s.png)
java的url方式、本地方式获取json文件内容
2023-08-22 18:30:23
解决运行jar包出错:ClassNotFoundException问题
2021-09-09 04:58:41
![](https://img.aspxhome.com/file/2023/6/75416_0s.png)
详解JAVA类加载机制
2023-11-08 04:05:16
![](https://img.aspxhome.com/file/2023/0/74910_0s.png)
Java 8新特性方法引用详细介绍
2023-06-22 08:31:50
Java操作FTP实现上传下载功能
2021-12-07 18:35:04
![](https://img.aspxhome.com/file/2023/1/82311_0s.png)
Android开发中使用颜色矩阵改变图片颜色,透明度及亮度的方法
2023-04-28 20:42:22
![](https://img.aspxhome.com/file/2023/6/90126_0s.jpg)
Java Volatile 变量详解及使用方法
2023-03-13 13:27:10
详解java Collections.sort的两种用法
2023-11-28 09:30:31
![](https://img.aspxhome.com/file/2023/3/60473_0s.png)
JDK8中新增的原子性操作类LongAdder详解
2023-06-19 22:02:58
![](https://img.aspxhome.com/file/2023/3/76303_0s.jpg)
C#检测远程计算机端口是否打开的方法
2023-12-11 08:55:31
ZooKeeper 实现分布式锁的方法示例
2023-03-20 07:26:43
![](https://img.aspxhome.com/file/2023/7/89847_0s.png)
android视频截屏&手机录屏实现代码
2022-12-10 15:20:31
![](https://img.aspxhome.com/file/2023/7/87537_0s.png)
C# WebApi Get请求方式传递实体参数的方法示例
2023-05-01 21:46:46
适配Android 8.0版本更新安装与通知栏的一些坑
2022-05-01 13:23:25
![](https://img.aspxhome.com/file/2023/3/90143_0s.jpg)
SpringBoot异步任务使用方法详解
2021-08-07 07:57:02
![](https://img.aspxhome.com/file/2023/4/70964_0s.png)