使用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的跳转和传值的更多相关文章

  1. Android开发10——Activity的跳转与传值

    Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一Intent intent = new Intent(A.this, B ...

  2. Activity的跳转与传值(转载)

    Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据.   Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...

  3. Activity的跳转与传值

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/323982 Acti ...

  4. xamarin.android Activity之间跳转与传值

    前言 由于需要,所以接触到这个新的安卓开发模式,我会把我的学习经历全都记录下来,希望对大家有用. 导读 关于Activity,学习过安卓的人也应该明白什么是Activity,推荐新手去看YZF的这篇文 ...

  5. Android课程---Activity的跳转与传值(转自网上)

    Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据.   Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...

  6. IOS学习[Swift中跳转与传值]

    Swift中页面跳转与传值: 1.简单方式 首先,Swift的跳转可分为利用xib文件跳转与storyboard跳转两种方法,我这里选择使用storyboard的界面跳转方法. 1.通过在storyb ...

  7. Android开发之Activity的创建跳转及传值

    在Android系统的江湖中有四大组件:活动(Activity), 服务(Service), 广播接收器(Broadcast Reciver)和内容提供者(Content Provider).今天所介 ...

  8. Activity学习(三)——跳转传值

    Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据.   Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...

  9. Android学习之Activity跳转与传值

    Activity跳转与传值,主要是通过Intent类,Intent的作用是激活组件和附带数据. 一.Activity跳转 方法一 Intent intent = new Intent(A.this, ...

随机推荐

  1. 课程设计-基于SSM的在线课程教学系统代码-基于java的线上课程资源共享论坛系统

    注意:该项目只展示部分功能,如需了解,评论区咨询即可. 1.开发环境 开发语言:Java 后台框架:SSM 前端框架:vue 数据库:MySQL 设计模式:MVC 架构:B/S 源码类型: Web 编 ...

  2. NOIP 模拟 $26\; \rm 神炎皇$

    题解 \(by\;zj\varphi\) 一道 \(\varphi()\) 的题. 对于一个合法的数对,设它为 \((a*m,b*m)\) 则 \(((a+b)*m)|a*b*m^2\),所以 \(( ...

  3. noip 模拟 7

    我花了我多久的rp啊-- 考试经过 这次是三道题,依旧先看一遍,然后从头开始做 T1一看,这好像是KMP?等等,我好像忘了啊你个废,没事哈希也能做,On似乎可以呀,一波操作,我是不是要A题了? 转到T ...

  4. 数据结构与算法-排序(十)桶排序(Bucket Sort)

    摘要 桶排序和基数排序类似,相当于基数排序的另外一种逻辑.它是将取值范围当做创建桶的数量,桶的长度就是序列的大小.通过处理比较元素的数值,把元素放在桶的特定位置,然后遍历桶,就可以得到有序的序列. 逻 ...

  5. React性能优化总结

    本文主要对在React应用中可以采用的一些性能优化方式做一下总结整理 前言 目的 目前在工作中,大量的项目都是使用react来进行开展的,了解掌握下react的性能优化对项目的体验和可维护性都有很大的 ...

  6. map中使用箭头函数遇到的坑

    箭头函数提供了更简洁和更短的语法,其中一个可用功能是可以将函数编写为具有隐式返回值的lambda表达式.这对于功能样式代码很方便,比如使用函数映射数组: const numbers = [1,2,3, ...

  7. springboot整合多数据源解决分布式事务

    一.前言        springboot整合多数据源解决分布式事务.             1.多数据源采用分包策略              2.全局分布式事务管理:jta-atomikos. ...

  8. .NET Core程序发布报错:project.assets.json”没有“.NETCoreApp,Version=v3.1/win-x64”的目标。确保已运行还原,且“netcoreapp3.1”已包含在项目的 TargetFrameworks中。

    在控制台中使用命令发布.NET Core程序的时候,报如下的错误: project.assets.json"没有".NETCoreApp,Version=v3.1/win-x64& ...

  9. springCloud实战

    背景 -------------------------学前必读---------------------------------- 学习不能快速成功,但一定可以快速入门整体课程思路:1.实践为主,理 ...

  10. Linux的wget命令详解

    [转载] Linux wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,尤其对于网络管理员,经常要下载一些软件或从远程服务器恢复备份到本地服务器.如果我们使用虚拟主机, ...