使用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. pthread_cleanup_push与pthread_cleanup_pop的理解

    一.为什么会有pthread_cleanup_push与pthread_cleanup_pop: 一般来说,Posix的线程终止有两种情况:正常终止和非正常终止.线程主动调用pthread_exit( ...

  2. spring学习08(声明式事务)

    11.声明式事务 11.1 回顾事务 事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎! 事务管理是企业级应用程序开发中必备技术,用来确保数据的完整性和一致性. 事务就是把一系列的动作当 ...

  3. python关于多级包之间的引用问题

    首先得明确包和模块. 包:在一个目录下存在__init__.py,那么该目录就是一个包. 模块:一个.py文件就是一个模块. 我们可以通过from 包 import 模块来引入python文件, 也可 ...

  4. kvm虚拟化的qcow2磁盘格式的扩容方法

    第一种:增加一块磁盘而另磁盘空间增大 1).先进入kvm环境,创建一块硬盘:qemu-img create -f qcow2 /home/tianke/test.qcow2 40G 2).再给增加的硬 ...

  5. Java Slf4j日志配置输出到文件中

    1.概述 新项目需要增加日志需求,所以网上找了下日志配置,需求是将日志保存到指定文件中.网上找了下文章,发现没有特别完整的文章,下面自己整理下. 1.Java日志概述 对于一个应用程序来说日志记录是必 ...

  6. Docker搭建Svn服务器

    一.下载镜像 # 搜索镜像 docker search svn # 下载镜像 docker pull garethflowers/svn-server 二.启动镜像 # 编辑配置文件 vim dock ...

  7. npm常用命令及其node相关工具汇总

    它是一个事件驱动异步I/O单进程的服务端JS环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好. 浏览器是JS的前端运行环境. Node.js是JS的后端运行环 ...

  8. spring cloud 知识总结

    ### 单体应用存在的问题 - 随着业务的发展,开发变得越来越复杂.- 修改.新增某个功能,需要对整个系统进行测试.重新部署.- 一个模块出现问题,很可能导致整个系统崩溃.- 多个开发团队同时对数据进 ...

  9. spring开发中常见错误集合,逐步添加

    1.关于jstl错误:原因,在jsp页面中使用了jstl标签库,但是却没有导入,可以将相应的jar包放在tomcat的lib目录下,一劳永逸 Java.lang.NoClassDefFoundErro ...

  10. 如何修改leaflet的marker图标

    1. 从官网中查看对应文档:https://leafletjs.com/ 2. 3. var greenIcon = L.icon({ iconUrl: 'leaf-green.png', shado ...