使用Bundle在Activity中交换数据
大概过程

编写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中交换数据的更多相关文章
- 使用 Bundle 在 Activity 之间交换数据
[toc] 使用 Bundle 在 Activity 之间交换数据 场景 当一个 Activity 启动另一个 Activity 时,常常会有一些数据需要传过去.因为两个 Activity 之间本来就 ...
- 建立、配置和使用Activity——使用Bundle在Activity之间交换数据
当一个Activity启动另一个Activity时,常常会有一些数据需要传过去——这就像Web应用从一个Servlet跳到另一个Serlvet时,Web应用习惯把需要交换的数据放入requestSco ...
- 使用Bundle在Activity之间交换数据
一:在main.xml文件中设置布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- 【Android 复习】:从Activity中返回数据
在实际的应用中,我们不仅仅要向Activity传递数据,而且要从Activity中返回数据,虽然返回数据和传递类似,也可以采用上一讲中的四种方式来传递数据,但是一般建议采用Intent对象的方式的来返 ...
- 从Activity中返回数据
从Activity中返回数据 一.简介 这里也就是使用intent方式返回数据. 二.具体步骤 在MainActivity通过一个button访问Activity01页面,然后将Activity01页 ...
- 使用Bundle在Activity间传递数据
使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...
- Android笔记——Activity中的数据传递案例(用户注冊)
1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
随机推荐
- vue项目中关闭eslint的方法
非常简单的操作方法!不用再去为了烦人的代码标准报错而苦恼了! 方法一:在项目根目录下增加 vue.config.js 文件 添加以下代码: module.exports = { lintOnSave: ...
- [PHP学习教程 - 网络]003.获得当前访问的页面URL(Current Request URL)
引言:获取当前请求的URL路径,自动判断协议(HTTP or HTTPS). 一句话的事情,下面直接上高清无MSK的精妙代码! 功能函数 获得当前请求的页面路径(URL)地址 语法:$url = ge ...
- Python数据分析:pandas玩转Excel(三)
将对象写入Excel工作表. 要将单个对象写入 Excel .xlsx 文件,只需指定目标文件名即可.要写入多个工作表,必须创建具有目标文件名的ExcelWriter对象,并在文件中指定要写入的工作表 ...
- 练习使用shell在阿里云安装MySQL
#!/bin/bash #阿里云初始安装MySQL #step1:查寻MariaDB 并卸载 MariaDB_filename=`rpm -qa|grep mariadb` if [ -d " ...
- 使用ADMT和PES实现window AD账户跨域迁移-介绍篇
使用 ADMT 和 pwdmig 实现 window AD 账户跨域迁移系列: 介绍篇 ADMT 安装 PES 的安装 ADMT:迁移组 ADMT:迁移用户 ADMT:计算机迁移 ADMT:报告生成 ...
- pip安装Python库速度慢的解决方法
最近在写大数据文本挖掘的考查报告,需要用到 jieba切词,于是在pycharm中安装 jieba 库 首先是在 File—settings中通过搜索安装,然而安了五分钟之后还是失败了 于是通过终端输 ...
- Java实现 LeetCode 793 阶乘函数后K个零 (分析)
793. 阶乘函数后K个零 f(x) 是 x! 末尾是0的数量.(回想一下 x! = 1 * 2 * 3 * - * x,且0! = 1) 例如, f(3) = 0 ,因为3! = 6的末尾没有0:而 ...
- Java实现 LeetCode 773 滑动谜题(BFS)
773. 滑动谜题 在一个 2 x 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示. 一次移动定义为选择 0 与一个相邻的数字(上下左右)进行交换. 最终 ...
- Java实现 蓝桥杯VIP 算法训练 入学考试
问题描述 辰辰是个天资聪颖的孩子,他的梦想是成为世界上最伟大的医师.为此,他想拜附近最有威望的医师为师.医师为了判断他的资质,给他出了一个难题.医师把他带到一个到处都是草药的山洞里对他说:" ...
- Java实现 LeetCode 202 快乐数
202. 快乐数 编写一个算法来判断一个数是不是"快乐数". 一个"快乐数"定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过 ...