使用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) ...
随机推荐
- centos7 安装rpm版的mysql遇到坑——误删root用户的恢复
在网上找了教程http://blog.csdn.net/frankcheng5143/article/details/77609093安装过程很顺利,随着修改了root的密码后不下心误删了root账号 ...
- mysql中 Rank、DENSE_RANK()的区别
相同点:RANK()和DENSE_RANK()的是排名函数 不同点:RANK()是跳跃排序,即如果有两条记录重复,接下来是第三级别 如:1 2 2 4,会跳过3 DENSE_RANK()是连续排序,即 ...
- RabbitMq和ZeroMq
RabbitMQ和ZeroMQ都是极好的消息中间件,下我会对这两个消息中间件做一个比較,个人理解不喜勿喷. RabbitMQ是AMQP协议率先的一个实现,它实现了代理(Broker)架构,意味着消息在 ...
- Eclipse中java文件选中变量名,相同变量都变色显示 .
第一步设置高亮显示的颜色: Window-->preferences-->General-->Editors-->Text Editors-->Annotations-- ...
- Jmeter(四) - 从入门到精通 - 创建网络测试计划(详解教程)
1.简介 在本节中,您将学习如何创建基本的 测试计划来测试网站.您将创建五个用户,这些用户将请求发送到JMeter网站上的两个页面.另外,您将告诉用户两次运行测试.因此,请求总数为(5个用户)x(2个 ...
- 使用turtle库绘制一个叠加等边三角形
import turtle as t t.setup(600, 600, None,None) t.pu() t.fd(-120) t.pensize(5) t.width(5) t.pencolor ...
- ASP.NET实现一个在线音乐统计网站(歌手,音乐,角色……增删改查)
这里更多的是当作随身笔记使用,记录一下学到的知识,以便淡忘的时候能快速回顾 当前步骤是该项目的完结部分(前面由于没有时间整理了,直接发一个大完结吧) 第一部分 第二部分 源码已上传GitHub:这里有 ...
- Java实现 蓝桥杯 算法训练 多阶乘计算
试题 算法训练 多阶乘计算 问题描述 我们知道,阶乘n!表示n*(n-1)(n-2)-21, 类似的,可以定义多阶乘计算,例如:5!!=531,依次可以有n!..!(k个'!',可以简单表示为n(k) ...
- c/c++混编
/* head.h */#ifndef __SUM_H__ #define __SUM_H__ #ifdef __cplusplus extern "C" { #endif int ...
- SOA架构和微服务架构的区别与特点
1.SOA架构和微服务架构的区别 首先SOA和微服务架构一个层面的东西,而对于ESB和微服务网关是一个层面的东西,一个谈到是架构风格和方法,一个谈的是实现工具或组件. 1.SOA(Service Or ...