大概过程

编写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. SQL——MySQL数据类型

    Text类型: Number类型: Date类型:

  2. css3新选择

    官方解释: [attribute^=value],a[src^="https"],选择其 src 属性值以 "https" 开头的每个 <a> 元素 ...

  3. PIC单片机的定时器

    PIC单片机的定时器有3个 timer0 timer1 timer2 定时器的计算方法 256*k*Tcy=定时时间 (256-Init-value)*k*Tcy=定时时间

  4. [Objective-C] 012_数据持久化_XML属性列表,NSUserDefaults

    在日常开发中经常要对NSString.NSDictionary.NSArray.NSData.NSNumber这些基本类的数据进行持久化,我们可以用XML属性列表持久化到.plist 文件中,也可以用 ...

  5. Java IO(二)File

    Java IO(二)File 一.概述 在 Java 中,File 类是 java.io 包中唯一代表磁盘文件本身的对象,也就是说,如果希望在程序中操作文件和目录,则都可以通过 File 类来完成.F ...

  6. 选择器&隔行换色

    选择器的使用理解为:执行jQuery核心函数,传入选择器的字符串    $( ... ) 基本选择器 <!DOCTYPE html> <html> <head> & ...

  7. 中国电信中兴F412光猫——IPTV与网络单线复用

    标题: 中国电信中兴F412光猫--IPTV与网络单线复用 作者: 梦幻之心星 347369787@QQ.com 标签: [光猫, IPTV] 目录: 路由器 日期: 2019-2-30 目录 第一步 ...

  8. WEB APPLICATION PENETRATION TESTING NOTES

    此文转载 XXE VALID USE CASE This is a nonmalicious example of how external entities are used: <?xml v ...

  9. Rocket - tilelink - TLArbiter

    https://mp.weixin.qq.com/s/0ob-Fq-ZOoj-_S7pTJu6rQ   介绍TLArbiter的实现,主要关注如何实现burst的多个beat的输出.   ​​   1 ...

  10. Java实现 LeetCode 775 全局倒置与局部倒置(分析题)

    775. 全局倒置与局部倒置 数组 A 是 [0, 1, -, N - 1] 的一种排列,N 是数组 A 的长度.全局倒置指的是 i,j 满足 0 <= i < j < N 并且 A ...