我们这次想要实现的功能是从A活动跳到B活动时,A活动中有一个输入框和一个按钮,当点击按钮是时会跳到B活动,然后把A活动中的输入框的内容传到B活动中,且在B活动中的TextView中显示

A活动中先添加一个输入框和一个按钮

代码如下

 <EditText
android:id="@+id/new_et"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转"
android:id="@+id/btn1"/>

然后在A活动中初始化这两个控件

 private Button btn1;
private EditText new_dt;
btn1=(Button)findViewById(R.id.btn1);
new_dt=(EditText)findViewById(R.id.new_et);

因为我们需要的功能是点击按钮跳转到B活动,所以需要在A活动中设置按钮监听器,并且在监听器中添加跳转所需要的代码

 Intent i=new Intent(NewActivity.this,ThirdActivity.class);//初始化Intent
i.putExtra("info",new_dt.getText().toString());//把输入框中的内容,传到info
startActivity(i);//启动活动

接下来我们接着完成B活动,在B活动中我们使用TextView来显示

我们把xml文件中我们添加TextView控件

 <TextView
android:id="@+id/third_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示内容"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="200dp"
/>

在B活动的Java中我们先初始化控件,然后接受A活动传回来的数据,并在TextView中显示

初始化控件

 private TextView tv;
tv=(TextView)findViewById(R.id.third_tv);

接下来使用getIntent来获取A活动传过来的Intend对象,代码如下

  if (getIntent()!=null){//判断是否由上个界面跳转过来
  Intent intent=getIntent();
String info=intent.getStringExtra("info");//获取info中的信息并且转化为String类型的
tv.setText(info);//如果不为空,则在TextView中显示info中的数据
}

如果需要大数据来进行传输的话,可以使用Bundle,并且适应extras,来进行添加和接受,可以把在A活动中按钮监听器中的代码换为如下代码

 Intent i=new Intent(NewActivity.this,ThirdActivity.class);//初始化Intent
Bundle bundle=new Bundle();//初始化Bundle集合
bundle.putString("info",new_dt.getText().toString());//把输入框中的字符串传给info
i.putExtras(bundle);//在intent对象中添加bundle
startActivity(i);//启动活动

在B活动中的fi代码可以换位如下代码:

  if (getIntent()!=null){//判断是否由上个界面跳转过来
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String info=bundle.getString("info");
tv.setText(info);//如果不为空,则在TextView中显示info中的数据
}

以上就是extra的知识

Intend之属性extra的更多相关文章

  1. 关于extra加强延迟加载

    一对多和多对多关联的查询策略 lazy属性的另一个属性extra 加强延迟加载 表明采用增强延迟加载策略:在<set>元素配置lazy属性为"extra".增强延迟加载 ...

  2. Hibernate 延迟加载

    一.什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象访 ...

  3. 安卓第十一天笔记-Intent与inter-filter配置

    安卓第十一天笔记-Intent与inter-filter配置 Intent与inter-filter配置 1.Intent对象简述 Android应用中有包含三种重要组件:Activity,Servi ...

  4. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  5. Javascript 装饰器极速指南

    pablo.png Decorators 是ES7中添加的JavaScript新特性.熟悉Typescript的同学应该更早的接触到这个特性,TypeScript早些时候已经支持Decorators的 ...

  6. jqu

    1 /*2 * 说明:3 * 本源代码的中文注释乃Auscarlin呕心沥血所作.旨在促进jQuery的传播以及向广大jQuery爱好者提供一个进阶4 *的途径,以让各位更加深入地了解jQuery,学 ...

  7. MySQL Explain学习笔记

    目录 一.执行计划概念 二.Explain用法 三.Explain属性介绍 3.1 id属性 3.2 select_type属性 3.3 table属性 3.4 type属性 3.5 possible ...

  8. Intent的属性及Intent-filter配置——Extra属性——Flag属性

    Intent的Extra属性通常用于在多个Action之间进行数据交换,Intent的Extra属性值应该是一个Bundle对象,Bundle对象的就像一个Map对象,它可以存入多组key-value ...

  9. Hibernate学习(五)lazy属性学习(true和extra区别)

    Lazy(懒加载)在hibernate何处使用:1.<class>标签上,可以取值:true/false,(默认值是:true)2.<property>标签上,可以取值:tru ...

随机推荐

  1. DataGrid DataGridTextColumn 樣式

    <DataGridTextColumn.ElementStyle> <Style TargetType="TextBlock" > <Setter P ...

  2. ABC156E

    题目链接 也是简单的组合数学问题,每个位置可以移动走,也可以移动来,那么我们就需要找最终的状态,也就是最终的0的个数 假设有m个0,就有n-m个非0空位,选择0的组合数为\(\textrm{C}_{n ...

  3. 4_5 追踪电子表格中的单元格(UVa512)(选做)

    在电子表格中的数据都存储在单元格中,它是按行和列(R)(C).一些在电子表格上的操作可以应用于单个单元格(研发),而其他的可以应用于整个行或列.典型的单元操作包括插入和删除行或列和交换单元格内容.一些 ...

  4. CSS3绘制不规则图形,代码收集

    三角形系列(三角形.倒三角.左三角.右三角.左上三角.右上三角.左下三角.右下三角) 主要用到的是:宽度高度设置为0, border的各个边的设置(各个边的透明或不透明): .triangle-up ...

  5. window下jenkins+allure+邮箱发送

    一.安装allure 1)下载 allure.zip 下载地址:allure-github: https://github.com/allure-framework/allure2 2)解压到本地目录 ...

  6. Introducing .NET 5

    Today, we’re announcing that the next release after .NET Core 3.0 will be .NET 5. This will be the n ...

  7. Numpy中rot90函数实现矩阵旋转

    从NumPy的官方完整查到rot90函数语法格式如下: rot90(m, k=1, axes=(0, 1) m是要旋转的数组(矩阵),k是旋转的次数,默认旋转1次,那是顺时针还是逆时针呢?正数表示逆时 ...

  8. Codeforces Round #588 (Div. 2)E(DFS,思维,__gcd,树)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;long long a[100007];vec ...

  9. [python]python中的一些包以及他们的基本用处和安装

    pika可以用来连接rabbitmqpip install pika -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com ...

  10. Vacuum Pump Manufacturer Introduction: Airless Pump Bottle

    Fillable vacuum pump bottle with matt silver aluminum base and cap and shiny silver aluminum collar. ...