5.10学习总结——Activity的跳转和传值
使用sharedpreference是对信息的存储,也可以进行传值,今天通过查找资料,学习了Activity的跳转和传值方法。
跳转
1、显示跳转
4种方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class AActivity extends AppCompatActivity { private Button ma; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); ma=findViewById(R.id.btn_a); ma.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显式方法1 Intent intent=new Intent(AActivity.this,BActivity.class); startActivity(intent); /*显式跳转方法2 Intent intent=new Intent(); intent.setClass(AActivity.this,BActivity.class); startActivity(intent); */ /*显式跳转方法3 Intent intent=new Intent(); intent.setClassName(AActivity.this,"com.example.textview.jump.BActivity"); startActivity(intent); */ /*显式跳转方法4 Intent intent=new Intent(); intent.setComponent(new ComponentName(AActivity.this,"com.example.textview.jump.BActivity")); startActivity(intent); */ } }); }} |
数据传递
AActivity点击跳转后发送数据
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
package com.example.textview.jump;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import com.example.textview.R;public class AActivity extends AppCompatActivity { private Button ma; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); ma=findViewById(R.id.btn_a); ma.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显式方法1 Intent intent=new Intent(AActivity.this,BActivity.class); //数值传递 Bundle bundle=new Bundle(); bundle.putString("name","刘亦菲");//bundle里放传输的数据 bundle.putInt("number",28); intent.putExtras(bundle);//bundle通过intent传过去 startActivity(intent); } }); }} |
BActivity接收数据
B的布局界面放一个TextView用来显式传输来的文件
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:padding="10dp" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="25dp" android:textColor="#F08080" android:id="@+id/text_1"/></LinearLayout> |
BActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package com.example.textview.jump;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import com.example.textview.R;public class BActivity extends AppCompatActivity { private TextView mTV; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); mTV=findViewById(R.id.text_1); Bundle bundle=getIntent().getExtras(); String name=bundle.getString("name"); int number=bundle.getInt("number"); mTV.setText(name+","+number); }} |
startActivityForResul
界面1跳转到界面2,界面2将得到的数据又转到界面1
AActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com.example.textview.jump;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import com.example.textview.R;public class AActivity extends AppCompatActivity { private Button ma; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); ma=findViewById(R.id.btn_a); ma.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显式方法1 Intent intent=new Intent(AActivity.this,BActivity.class); //数值传递 Bundle bundle=new Bundle(); bundle.putString("name","刘亦菲");//bundle里放传输的数据 bundle.putInt("number",28); intent.putExtras(bundle);//bundle通过intent传过去 // startActivity(intent); startActivityForResult(intent,0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); Toast.makeText(this, data.getExtras().getString("title"), Toast.LENGTH_SHORT).show(); }} |
BActivity.java
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.example.textview.jump;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import com.example.textview.R;public class BActivity extends AppCompatActivity { private TextView mTV; private Button mab; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); mTV=findViewById(R.id.text_1); mab=findViewById(R.id.ab_1); Bundle bundle=getIntent().getExtras(); String name=bundle.getString("name"); int number=bundle.getInt("number"); mTV.setText(name+","+number); mab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); Bundle bundle1=new Bundle(); bundle1.putString("title","宝宝回啦了"); intent.putExtras(bundle1); setResult(Activity.RESULT_OK,intent); finish(); } }); }} |
5.10学习总结——Activity的跳转和传值的更多相关文章
- Android开发10——Activity的跳转与传值
Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一Intent intent = new Intent(A.this, B ...
- Activity的跳转与传值(转载)
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- Activity的跳转与传值
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/323982 Acti ...
- xamarin.android Activity之间跳转与传值
前言 由于需要,所以接触到这个新的安卓开发模式,我会把我的学习经历全都记录下来,希望对大家有用. 导读 关于Activity,学习过安卓的人也应该明白什么是Activity,推荐新手去看YZF的这篇文 ...
- Android课程---Activity的跳转与传值(转自网上)
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- IOS学习[Swift中跳转与传值]
Swift中页面跳转与传值: 1.简单方式 首先,Swift的跳转可分为利用xib文件跳转与storyboard跳转两种方法,我这里选择使用storyboard的界面跳转方法. 1.通过在storyb ...
- Android开发之Activity的创建跳转及传值
在Android系统的江湖中有四大组件:活动(Activity), 服务(Service), 广播接收器(Broadcast Reciver)和内容提供者(Content Provider).今天所介 ...
- Activity学习(三)——跳转传值
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- Android学习之Activity跳转与传值
Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一 Intent intent = new Intent(A.this, ...
随机推荐
- openssl常用命令行汇总
openssl常用命令行汇总 随机数 openssl rand -out rand.dat -base64 32 摘要 直接做摘要 openssl dgst -sha1 -out dgst.dat p ...
- HTML5(十一)——WebSocket 基础教程
一.为什么要学 WebSocket? websocket 是 HTML5 提供的一种长链接双向通讯协议,使得客户端和服务器之间的数据交换更简单,允许服务端主动向客户端推送数据,并且客户端与服务端只需连 ...
- Convert a Private Project on bitbucket.com to a github Public Project
Create a public repo on github, you can add README or License files on the master branch, suppose th ...
- Git-07-分支管理
创建与合并分支 为什么要创建分支? 假设你准备开发一个新功能,但是需要两周才能完成, 第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了. 如果等代码全部写完 ...
- DVWA靶场之SQL injection(blind)通关
盲注,顾名思义,无法从界面上直接查看到执行结果,一般的SQL注入基本绝迹,最多的就是盲注 基本步骤:(由于没有回显了,相比一般的SQL注入,也就不需要确定查询字段数.判断回显位置了) 判断注入类型 破 ...
- SpringBoot集成Druid
maven <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-b ...
- Longhorn,企业级云原生容器分布式存储 - 支持 ReadWriteMany (RWX) 工作负载(实验性功能)
内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 企业级云原生容器分布式存储解决方案设计架构和概念 Longhorn 企业级云原生容器分 ...
- 使用JS获取SessionStorage的值
参考:https://www.jb51.net/article/132729.htm 获取sessionStorage的意义 首先获取它是为了将获得的信息输出或者alert():让人容易看到, 其次, ...
- 设计模式<一>
设计原则1.找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起. 2.针对接口编程,而不是针对实现编程. 3.多用组合,少用继承. 一:策略模式,定义了算法族,分别封装起来 ...
- Linux centos 安装 jenkins & 本地构建jar & 远程构建jar
一.部署 jenkins 需要的前奏 1.安装 JDK:https://www.cnblogs.com/chuyi-/p/10644440.html 2.安装tomcat:https://www.cn ...