使用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. Python3中的字符串相关操作

    Python3的字符串操作相关函数详解 字符串内建函数 1. capitalize() 将字符串中的第一个字符转换成大写,其他字母变成小写.例: >>> "hello Wo ...

  2. Spring IOC容器核心流程源码分析

    简单介绍 Spring IOC的核心方法就在于refresh方法,这个方法里面完成了Spring的初始化.准备bean.实例化bean和扩展功能的实现. 这个方法的作用是什么? 它是如何完成这些功能的 ...

  3. windows运行Tomcat配置自定义的jdk环境运行。

    找到tomcat下的bin目录下文件 setclasspath.bat 第二行加上你想运行的jdk路径即可. set "JRE_HOME=D:\Program Files (x86)\jdk ...

  4. SQL 练习41

    编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水.如果不存在第二高的薪水,那么查询应返回 ...

  5. 【现学现卖】python小爬虫

    1.给小表弟汇总一个院校列表,想来想去可以写一个小爬虫爬下来方便些,所以就看了看怎么用python写,到了基本能用的程度,没有什么特别的技巧,大多都是百度搜的,遇事不决问百度啦 2.基本流程就是: 用 ...

  6. Vue实现在前端导出Excel 方法2

    也可以去看下我的方法1:https://www.cnblogs.com/yingyigongzi/p/10915382.html ----------------------------------- ...

  7. spring加载Bean的解析过程(二)

    1.例如: BeanFactory bf = new XmlBeanFactory(new ClassPathResource("spring.xml")); User user ...

  8. docker安装与配置gitlab详细过程

    docker安装与配置gitlab详细过程 1.打开网易镜像中心 https://c.163yun.com/hub#/m/home/ 2.搜索gitlab,获取下载地址.例如:docker pull  ...

  9. ES6 promise的应用

    html部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  10. Java并发之AQS原理解读(三)

    上一篇:Java并发之AQS原理解读(二) 前言 本文从源码角度分析AQS共享锁工作原理,并介绍下使用共享锁的子类如何工作的. 共享锁工作原理 共享锁与独占锁的不同之处在于,获取锁和释放锁成功后,都会 ...