使用Bundle在Activity之间交换数据
一:在main.xml文件中设置布局
<LinearLayout 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"
android:orientation="vertical"
android:background="@drawable/background7" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user" />
<EditText
android:id="@+id/user"
android:layout_width="207dp"
android:layout_height="wrap_content"
android:hint="" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" />
<EditText
android:id="@+id/pwd"
android:layout_width="203dp"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/repass" />
<EditText
android:id="@+id/repwd"
android:layout_width="203dp"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email" />
<EditText
android:id="@+id/email"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:hint="" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="@string/title"
/>
</LinearLayout>
二:在主活动的Activity中进行信息的处理
package com.org.bundle_ac;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)findViewById(R.id.button1);//获取提交按钮
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String user=((EditText)findViewById(R.id.user)).getText().toString();//获取输入的用户名
String pwd=((EditText)findViewById(R.id.pwd)).getText().toString();//获取输入的密码
String repwd=((EditText)findViewById(R.id.repwd)).getText().toString();//获取输入的确认密码
String email=((EditText)findViewById(R.id.email)).getText().toString();//获取输入的email地址
if(!"".equals(user)&&!"".equals(pwd)&&!"".equals(email)){//进行判断
if(!pwd.equals(repwd)){
//如果两次输入的密码不一次给以提示消息并获得焦点
Toast.makeText(MainActivity.this, "两次输入的密码不一致,请重新输入!", Toast.LENGTH_SHORT).show();
((EditText)findViewById(R.id.pwd)).setText("");//清空密码编辑框
((EditText)findViewById(R.id.repwd)).setText("");//清空确认密码编辑框
((EditText)findViewById(R.id.pwd)).requestFocus();//让密码编辑框获得焦点
}else{
//将输入的信息保存的到Bundle中,并开启一个新的Activity显示输入的用户注册信息
Intent intent=new Intent(MainActivity.this,RegisterActivity.class);//
Bundle bundle=new Bundle();//创建并实例化一个Bundle对象
bundle.putCharSequence("user", user);//保存用户名
bundle.putCharSequence("pwd", pwd);//保存密码
bundle.putCharSequence("email", email);//保存email地址
intent.putExtras(bundle);//将Bundle对象添加到Intent对象中
startActivity(intent);//启新的Activity
}
}else{
Toast.makeText(MainActivity.this, "请将注册信息输入完整!", Toast.LENGTH_SHORT).show();
}
}//
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
三:在layout目录下创建名为register.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"
android:background="@drawable/background7">
<TextView
android:id="@+id/textView1"
android:layout_width="210dp"
android:layout_height="30dp"
android:text="@string/user" />
<TextView
android:id="@+id/textView2"
android:layout_width="210dp"
android:layout_height="30dp"
android:text="@string/password" />
<TextView
android:id="@+id/textView3"
android:layout_width="210dp"
android:layout_height="30dp"
android:text="@string/email" />
</LinearLayout>
四:创建一个RegisterActivity类,并进行信息的接收工作
package com.org.bundle_ac;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class RegisterActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);//设置该Activity要显示的内容视图
Intent intent=getIntent();//获取Intent对象
Bundle bundle=intent.getExtras();//获取传递的数据包
TextView user=(TextView)findViewById(R.id.textView1);//获取到显示用户名的TextView组件
user.setText("用户名:"+bundle.getString("user"));//获取输入的用户名并显示到TextView组件中
TextView pwd=(TextView)findViewById(R.id.textView2);//获取到显示密码的TextView组件
pwd.setText("密码:"+bundle.getString("pwd"));//获取输入的密码并显示到TextView组件中
TextView email=(TextView)findViewById(R.id.textView3);//获取到显示email的TextView组件
email.setText("E-mail:"+bundle.getString("email"));//获取输入的email并显示到TextView组件中
}
}
五:在AndroidManifest.xml文件中加入配置Activity的信息
<activity
android:name=".RegisterActivity"
android:label="@string/title"
android:icon="@drawable/p1"
></activity>
六:string的文件下的
字符串的配置
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Bundle_Ac</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="user">用户名:</string>
<string name="password">密码:</string>
<string name="repass">确认密码:</string>
<string name="email">Email地址:</string>
<string name="button1">提交</string>
<string name="title">提交</string>
</resources>
最后加入背景图片:
使用Bundle在Activity之间交换数据的更多相关文章
- 使用 Bundle 在 Activity 之间交换数据
[toc] 使用 Bundle 在 Activity 之间交换数据 场景 当一个 Activity 启动另一个 Activity 时,常常会有一些数据需要传过去.因为两个 Activity 之间本来就 ...
- 建立、配置和使用Activity——使用Bundle在Activity之间交换数据
当一个Activity启动另一个Activity时,常常会有一些数据需要传过去——这就像Web应用从一个Servlet跳到另一个Serlvet时,Web应用习惯把需要交换的数据放入requestSco ...
- 使用Bundle在Activity中交换数据
大概过程 编写demo activity_main.xml <?xml version="1.0" encoding="utf-8"?> <R ...
- Intent、Bundle——实现Activity之间的通信
http://blog.sina.com.cn/s/blog_62dea93001015847.html 一个应用程序会有多个Activity,但是只有一个Activity作为程序的入口,应用中的其他 ...
- Activity之间的数据传递-android学习之旅(四十七)
activity之间的数据传递主要有两种,一种是直接发送数据,另一种接受新启动的activity返回的数据,本质是一样的 使用Bundle传递数据 Intent使用Bundle在activity之间传 ...
- activity之间的数据传递方法
1 基于消息的通信机制 Intent--------boudle,extra 用这种简单的形式,一般而言传递一些简单的类型是比较容易的,如int.string等 详细介绍下Intent机制 Inte ...
- Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)
Fragment与Activity之间的数据交换,通常含有3: 一.Fragment从Activity获取数据(仅本文介绍了一个第一): 两.Activity从Fragment获取数据: 三.Frag ...
- Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
随机推荐
- C# .NET ASP.NET 其中关系你了解多少
有些人一直在做这方面..但突然有人来问你这些问题..估计有很多答不上来. 1..NET是一个平台,一个抽象的平台的概念. .NET平台其本身实现的方式其实还是库,抽象层面上来看是一个平台. 个人理解. ...
- tomcat加载类的顺序
/bin:存放启动和关闭tomcat的脚本文件: /conf:存放tomcat的各种配置文件,比如:server.xml /server/lib:存放tomcat服务器所需要的各种jar文件(jar文 ...
- JUnit4教程-高速入口
前言 大学刚学java当听说JUnit该,单元测试框架,使用非常简单的测试框架,JUnit测试测试框架将更加方便,easy.编写測试代码也是简单.明了,功能强大.今天我给大家简单分享一下最新JUnit ...
- PhotoShop CC安装抠图插件KnockOut 2
1.KnockOut 2只有32位版本,因此需要给32位的PhotoShop CC安装. 2.下载地址:http://www.cr173.com/soft/28207.html 3.安装KnockOu ...
- C# 通过扩展WebBrowser捕获网络连接错误信息
想捕获WebBrowser连接指定网站过程中发生的错误信息,包括网络无法连接.404找不到网页等等错误!经过网上的搜集,找到了以下解决方案,该解决方案不会在网站连接前发出多余的测试请求. 向Webbr ...
- Mac OSX操作系统安装和配置Zend Server 6教程(4)
在前三节中,完成了安装和配置,最后是登录后台,并设置权限.Zend server后台登录默认端口是10081.浏览器中的的地址应输入http://localhost:10081/ZendServer. ...
- js获取非行间样式或定义样式
<!--DOCTYPE html--> <html> <head> <meta charset="utf-8" /> <sty ...
- mysql的事物
所谓的事物就是一组原子性的SQL语句,或者说是一个独立的工作单元. 1.事物拥有四大特征: ①原子性(atomicity):一个事物必须被分为一个不可分割的的最小单元,整个事物中的所有操作要么全部提交 ...
- sql语句 面试题
ql语句 面试题 自动编号 学号 姓名 课程编号 课程名称 分数 1 2005001 张三 0001 数学 69 2 2005002 李四 ...
- jQuery插件的编写相关技术 设计总结和最佳实践
原文:http://www.itzhai.com/jquery-plug-in-the-preparation-of-related-technical-design-summary-and-best ...