大概过程

编写demo

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#ff0000"
android:text="@string/intruduce"/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入姓名"
android:textColor="#000"
android:layout_below="@+id/top"/> <EditText
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入所在城市"
android:textColor="#000"
android:layout_below="@+id/name"/> <EditText
android:id="@+id/sex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入性别"
android:textColor="#000"
android:layout_below="@+id/city"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入电话号码"
android:textColor="#000"
android:layout_below="@+id/sex"/> <EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="请输入年龄"
android:textColor="#000"
android:layout_below="@+id/phone"/>
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#ff5000"
android:text="提交"/>
</RelativeLayout>

activity_result.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ResultActivity">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="朱归强"
android:textColor="#000" />
<TextView
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:text="男"
android:layout_below="@+id/name"/> <LinearLayout
android:id="@+id/agel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/sex">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:text="年龄:"/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="23"
android:textColor="#000"
/>
</LinearLayout> <LinearLayout
android:id="@+id/cityl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/agel">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:text="所在城市:"/>
<TextView
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="长沙"
android:textColor="#000"
/>
</LinearLayout> <LinearLayout
android:id="@+id/phonel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/cityl">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textColor="#000"
android:text="电话:"/>
<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="13155115510"
android:textColor="#000"
/>
</LinearLayout> <TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:textSize="26sp"
android:textColor="#F31D0B"
android:text="今日幸运值:60%"/>
</RelativeLayout>

MainActivity.java

package com.example.bundledemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnSubmit = (Button) findViewById(R.id.btn_submit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = ((EditText)findViewById(R.id.name)).getText().toString();
String sex = ((EditText) findViewById(R.id.sex)).getText().toString();
String age = ((EditText) findViewById(R.id.age)).getText().toString();
String city = ((EditText) findViewById(R.id.city)).getText().toString();
String phone = ((EditText) findViewById(R.id.phone)).getText().toString();
if(!"".equals(name)&&!"".equals(sex)&&!"".equals(age)&&!"".equals(city)&&!"".
equals(phone)){
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("name",name);
bundle.putCharSequence("sex",sex);
bundle.putCharSequence("age",age);
bundle.putCharSequence("city",city);
bundle.putCharSequence("phone",phone);
intent.putExtras(bundle);
startActivity(intent);
}else {
Toast.makeText(MainActivity.this, "请将信息填写完整", Toast.LENGTH_SHORT).show();
}
}
});
}
}

ResultActivity.java

package com.example.bundledemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView; public class ResultActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Intent intent = getIntent();
Bundle bundle = intent.getExtras(); String name = bundle.getString("name");
String sex = bundle.getString("sex");
String age = bundle.getString("age");
String city = bundle.getString("city");
String phone = bundle.getString("phone"); TextView tv_name = (TextView) findViewById(R.id.name);
TextView tv_sex = (TextView) findViewById(R.id.sex);
TextView tv_age = (TextView) findViewById(R.id.city);
TextView tv_city = (TextView) findViewById(R.id.age);
TextView tv_phone = (TextView) findViewById(R.id.phone); tv_name.setText(name);
tv_sex.setText(sex);
tv_age.setText(age);
tv_city.setText(city);
tv_phone.setText(phone);
}
}

效果:

使用Bundle在Activity中交换数据的更多相关文章

  1. 使用 Bundle 在 Activity 之间交换数据

    [toc] 使用 Bundle 在 Activity 之间交换数据 场景 当一个 Activity 启动另一个 Activity 时,常常会有一些数据需要传过去.因为两个 Activity 之间本来就 ...

  2. 建立、配置和使用Activity——使用Bundle在Activity之间交换数据

    当一个Activity启动另一个Activity时,常常会有一些数据需要传过去——这就像Web应用从一个Servlet跳到另一个Serlvet时,Web应用习惯把需要交换的数据放入requestSco ...

  3. 使用Bundle在Activity之间交换数据

    一:在main.xml文件中设置布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  4. 【Android 复习】:从Activity中返回数据

    在实际的应用中,我们不仅仅要向Activity传递数据,而且要从Activity中返回数据,虽然返回数据和传递类似,也可以采用上一讲中的四种方式来传递数据,但是一般建议采用Intent对象的方式的来返 ...

  5. 从Activity中返回数据

    从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...

  6. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  7. Android笔记——Activity中的数据传递案例(用户注冊)

    1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  8. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  9. Activity之间传递数据的方式及常见问题总结

    Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...

随机推荐

  1. nginx四种均衡策略

    1.基于轮询的均衡策略: 轮询嘛,就是说对进到nginx的request按照遍历的方式进行分发,如果request 1 分发到 Server A,那么request 2将被分发到 Server B,. ...

  2. C语言关于数据类型转换

    自动类型转换 自动类型转换就是编译器默默地.隐式地.偷偷地进行的数据类型转换,这种转换不需要程序员干预,会自动发生. 1) 将一种类型的数据赋值给另外一种类型的变量时就会发生自动类型转换,例如: ; ...

  3. kubeadm 搭建kubernetes集群环境

    需求 kubeadm 搭建kubernetes集群环境 准备条件 三台VPS(本文使用阿里云香港 - centos7.7) 一台能SSH连接到VPS的本地电脑 (推荐连接工具xshell) 安装步骤 ...

  4. [JavaWeb基础] 007.Struts2的配置和简单使用

    1.框架简介 采用Struts能开发出基于MVC(Model-View-Controller)设计模式的应用构架,用于快速开发Java Web应用.Struts实现的重点在C(Controller), ...

  5. 应用4:利用Filter限制用户浏览权限

    1. 使用 Filter 完成一个简单的权限模型: 1). 需求: ①. 管理权限 > 查看某人的权限 > 修改某人的权限 ②. 对访问进行权限控制: 有权限则可以访问, 否则提示: 没有 ...

  6. Python编程基本规范

    1.命名规范 类:类的名称一般为名词,且以驼峰形式(即每个单词首字母要大写,其余字母小写,单词之间无间隔符号)给出. 函数:一般以动词开头,函数名称要准确.简要地概括本函数的作用.函数名一律小写,如有 ...

  7. BZOJ1003 物流运输 题解

    发现\(n,m\)很小,我们可以先把任意\(2\)天的最短路都给求出来,考虑\(DP\),设\(f[i][j]\)表示\(j+1\)~ \(i\)这几天内走的是最短路线的最优方案,显然最优情况下\(j ...

  8. 【Java8新特性】关于并行流与串行流,你必须掌握这些!!

    写在前面 提到Java8,我们不得不说的就是Lambda表达式和Stream API.而在Java8中,对于并行流和串行流同样做了大量的优化.对于并行流和串行流的知识,也是在面试过程中,经常被问到的知 ...

  9. 五、Spring Web应用程序构建

    内容 映射请求到Spring控制器 透明地绑定表单参数 校验表单提交 关键词 模型-视图-控制器(Model-View-Controller,MVC) 处理器映射(handle mapping) 视图 ...

  10. Ubuntu18.04兼容Python2.7、Python3.6、Python3.8以及pip、pip2、pip3问题

    Ubuntu18.04兼容Python2.7.Python3.6.Python3.8以及pip.pip2.pip3问题 此为记录我重装Ubuntu后安装Python的过程 安装Python3.8 目前 ...