android studio for android learning (十一) 利用bundle在activity之间进行数据传递示例
1.该实例包含两个activity,一个是MainActivi.java和布局文件activity_main.xml,主要用来收集用户的注册信息,另一个是SecondActivity.java与second.xml用来显示第一个activity中提交的结果。(本示例参考疯狂android讲义),下面分别是各个示例的代码和布局文件。
//MainActivity.java
package com.dragon.testevent;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button bn = (Button) findViewById(R.id.bn);bn.setOnClickListener(new View.OnClickListener(){public void onClick(View v){EditText name = (EditText) findViewById(R.id.name);EditText passwd = (EditText) findViewById(R.id.passwd);RadioButton male = (RadioButton) findViewById(R.id.male);String gender = male.isChecked()?"male":"female";Person p = new Person(name.getText().toString(),passwd.getText().toString(),gender);Bundle data = new Bundle();data.putSerializable("person",p);Intent intent = new Intent(MainActivity.this,SecondActivity.class);intent.putExtras(data);startActivity(intent);}});}
}
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.dragon.testevent.MainActivity"><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="please input your information"android:textSize="20sp"/><TableRow><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="user name:"android:textSize="20sp"/><EditText
android:id="@+id/name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="please input you account"android:selectAllOnFocus="true"/></TableRow><TableRow><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="password"android:textSize="16sp"/><EditText
android:id="@+id/passwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:password="true"android:selectAllOnFocus="true"/></TableRow><TableRow><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:text="gender:"android:textSize="16sp"/><RadioGroup>android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"<RadioButton
android:id="@+id/male"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="male"android:textSize="16sp"/><RadioButton
android:id="@+id/female"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="female"android:textSize="16sp"/></RadioGroup></TableRow><Button
android:id="@+id/bn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="regster"android:textSize="16sp"/>
</TableLayout>
//Person.java
//Person是一个简单的DTO(Data Transfer Object(数据传输对象))对象,简单的实现了java.io.serializable中的接口
//相对应的还有一个DAO(Data Access Object(数据访问对象))
package com.dragon.testevent;import java.io.Serializable;/*** This file created by dragon on 2016/7/4 21:26,belong to com.dragon.testevent .*/
public class Person implements Serializable {private Integer id;private String name;private String passwd;private String gender;public Person(String name,String passwd,String gender){this.name = name;this.passwd = passwd;this.gender = gender;}public Integer getId(){return id;}public void setId(Integer id){this.id =id;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getPasswd(){return passwd;}public void setPasswd(String passwd){this.passwd = passwd;}public String getGender(){return gender;}public void setGender(String gender){this.gender= gender;}}
//SecondActivity.java
package com.dragon.testevent;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;/*** This file created by dragon on 2016/7/4 19:46,belong to com.dragon.testevent .*/
public class SecondActivity extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.second);TextView name = (TextView) findViewById(R.id.name);TextView passwd =(TextView) findViewById(R.id.passwd);TextView gender = (TextView) findViewById(R.id.gender);Intent intent = getIntent();Person p = (Person) intent.getSerializableExtra("person");name.setText("your user name is :"+ p.getName());passwd.setText("your password is :"+p.getPasswd());gender.setText("your gender is :"+p.getGender());}
}
//second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextView
android:id="@+id/name"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"/><TextView
android:id="@+id/passwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"/>
<TextView
android:id="@+id/gender"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"/></LinearLayout>
2.效果展示:
第一个activity
第二个activity
android studio for android learning (十一) 利用bundle在activity之间进行数据传递示例相关推荐
- Android中Activity之间的数据传递(Intent和Bundle)
当一个Activity启动另一个Activity时,常常会有一些数据传过去,对于Activity之间的数据交换更简单,因为两个Activity之间进行数据传递交换更简单,因为两个Activity之间本 ...
- android activity之间传递对象,Android Activity之间的数据传递
一.通过startActivity来进行Activity的传值 在Android中,如果我们要通过一个Activity来启动另一个Activity,可以使用 startActivity(Intent ...
- 安卓开发学习笔记(1)使用Bundle在Activity之间交换数据(实例:模拟淘宝的填写并显示收货地址的功能)
运行结果: 代码截图: 不要忘记在manifest->AndroidManifest.XML中添加新的Activity,否则会闪退 MainActivity.java AddressActivi ...
- Android之一窥究竟Activity间的数据传递以及Intent的用处
1.Activity与Intent * 1.1何为Intent * 1.2Intent的用武之地 * 1.3Activity间的数据传递 转载请标明出处: http://blog.csdn.net/h ...
- android 打印流程图,Android实现Activities之间进行数据传递的方法
本文实例讲述了Android实现Activities之间进行数据传递的方法.分享给大家供大家参考.具体分析如下: 首先,先说明一下Activity的启动及关闭: 1. startActivity(In ...
- Android Studio 之 Live Templates 高效利用
Android Studio 之 Live Templates 高效利用 在编辑器中使用用Ctrl + J 快捷键可以调出Live Templates,可以自动补全所设置模板的代码,善用它能够在很大程 ...
- 使用Android Studio搭建Android集成开发环境
一.Android Studio简单介绍 2013年GoogleI/O大会首次发布了Android Studio IDE(Android平台集成开发环境).它基于Intellij IDEA开发环境,旨 ...
- 阿里云+Android Studio+sql数据库实现增改删查与云端登录数据交互
本文使用阿里云.Navicat与Android Studio实现安卓app端对云端sql数据库的简单管理操作. 对于使用MySQL作为服务器的电脑也可以参照部分内容进行操作. 目录 云端服务器配置(阿 ...
- Android Studio打开Android Device Monitor提示An error has occurred. See the log file......
工具 Android Studio 3.0.1 问题描述:当我们打开Android Studio的Android Device Monitor时,发现出现弹窗,提示:An error has occu ...
- android+图标+i_explore+无背景,Android Studio中Android Device Monitor中的File Explore不显示文...
环境:操作系统是Mac,模拟器 问题:Android Studio中Android Device Monitor中的File Explore不显示文件 本人在自学文件存储,想查看"dada/ ...
最新文章
- 深度学习运行python文件,出现跨路径搜索其他module,怎么解决?
- jQuery:1.5.4.3,表格变色(单击行,把当行的单选按钮(radio)设为选中状态,并应用当前样式)...
- 树莓派pwm驱动好盈电调及伺服电机
- 如何用DOS命令查看占用某端口的程序及PID号
- pytorch 回归预测(时间序列)
- 2.窗口大小改变和侧边栏收缩的时候,echarts图表的自适应
- Redis面经:重新梳理了 5 种数据类型的用法和应用场景
- VS Code 实用快捷键
- 【算法】03 SCE-UA算法C++实现
- 关于Windows Server 2008 Service Pack 1
- 看电视剧《包青天》笔记
- 查询-SPJ练习2参考答案
- 容器化技术最佳实践1--容器化技术简介与Docker入门
- 面向端到端的情感对话生成研究综述
- 编程之美之一摞烙饼的排序1
- 过渡篇(1),初步了解Java泛型
- 什么是Portable Version?
- 云服务器和虚拟主机有什么区别?哪个比较好呢?
- 触发器实现当一个表中字段改变时同时改变另一个表中字段值
- PowerBI Report Server 集成,自定义身份验证,数据权限控制
热门文章
- android RelativeLayout 动态添加子View
- LaTeX使用小结2
- 我眼中的2011年互联网大事记
- wordpress如何获得当前用户的头像
- Pentium的保护工作方式
- 学计算机的感想300字,大学生计算机实训心得体会3篇
- springboot开启缓存_springBoot与缓存使用
- python图像颜色反转_OpenCV图像颜色反转算法详解
- 变速恒频风电机组的优缺点_风电消防安全解决方案解析
- 考研调剂 计算机科学 软件,2020年四川大学计算机学院(软件学院)考研调剂信息...