使用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. Sqli-Labs less54-65

    less-54 从54关开始,就是提升巩固的关卡,并且开始慢慢偏向实际. 第54关就是对输入的次数做了限制,需要在十次之内获取信息,否则就会刷新表名列名等信息. 以下的步骤截图就直接从上帝视角截图说明 ...

  2. DNS的正向解析与反向解析

    DNS域名解析服务(Domain Name System)是用于解析域名与IP地址对应关系的服务,功能上可以实现正向解析与反向解析: 正向解析:根据主机名(域名)查找对应的IP地址. 反向解析:根据I ...

  3. SpringBoot请求日期参数异常(Failed-to-convert-value-of-type-'java-lang-String'-

    问题 Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exce ...

  4. python的GUI框架tkinter,实现程序员的流氓式表白逻辑

    导入依赖 '''导入依赖''' import tkinter as tk import tkinter.messagebox as msg 创建并隐藏根窗口 '''创建并隐藏根窗口''' root_w ...

  5. sql 中的with 语句使用

    一直以来都很少使用sql中的with语句,但是看到了一篇文章中关于with的使用,它的确蛮好用,希望以后记得使用这个语句.一.with 的用法With alias_name as (select1)[ ...

  6. 【小技巧】java的List分页

    今天,工作上,由于业务的一些特殊性,需要拿到数据后在java代码中进行分页. 写了一个工具类,记录如下: import java.util.ArrayList; import java.util.Li ...

  7. 十八:使用JDBC进行批处理

    一.使用Statement完成批处理 1.使用Statement对象添加要批量执行SQL语句,如下: 1 Statement.addBatch(sql1); 2 Statement.addBatch( ...

  8. swagger2 注解说明文档

    @Api:用于类上,说明该类的作用.可以标记一个Controller类做为swagger 文档资源 @Api(value = "xxx", description = " ...

  9. Spring详解------概述

    1.什么是 Spring ? Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2E ...

  10. 大数据学习之 LINUX

    ##大数据学习 古斌6.6 01. linux系统的搭建: 选用 Contos 6.5 x64位系统 (CentOS-6.5-x86_64-minimal.iso) 我选择的为迷你版 模板机: bla ...