使用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) ...
随机推荐
- 给Notepad++ 6.7 加右键菜单带图标
使用的是Notepad++ 6.7,下载 NppShell64.dll 和 NppShell.dll方法:将BAT文件和下载的NppShell64.dll 和 NppShell.dll放置Notepa ...
- C#操作Word文档(加密、解密、对应书签插入分页符)
原文:C#操作Word文档(加密.解密.对应书签插入分页符) 最近做一个项目,客户要求对已经生成好的RTF文件中的内容进行分页显示,由于之前对这方面没有什么了解,后来在网上也找了相关的资料,并结合自己 ...
- 用bat启动sqlserver服务
声明下这个脚本不是我写的,忘了是从哪看到的了,在此分享给大家,因为在我的理解中技术就是用来分享的,,希望原创作者看到了不要介意. 1.创建个文本,将后缀名改成.bat 2.将下边语句粘贴进去,然后保存 ...
- W5500问题集锦(一)
在"WIZnet杯"以太网技术竞赛中,有非常多參赛者在使用中对W5500有各种各样的疑问,对于这款WIZnet新推出的以太网芯片,使用中大家是不是也一样存在下面问题呢?来看一看: ...
- 推荐两个针对github的chrome插件
作为一只程序猿,在github上找代码.看代码是再正常不过的事情了.这时候有个工具可以方便你翻看代码,想必是极好的. Sourcegraph for GitHub 这个插件允许你像使用IDE那样浏览代 ...
- php 链接中加参数传递
原文:php 链接中加参数传递 php链接中加参数是在源链接中加"?",问号之后就可以跟参数列表,para1=value1¶2=value2¶3=v ...
- php调用webservice报错Class 'SoapClient' not found
原文:php调用webservice报错Class 'SoapClient' not found php在调用webservice时,报告如下类似错误: ( ! ) Fatal error: Clas ...
- 我的Android 4 学习系列之创建用户基本界面
目录 使用视图和布局 理解Fragment 优化布局 创建分辨率无关的用户界面 扩展.分组.创建和使用视图 使用适配器将数据绑定到视图 使用视图和布局 1. Android UI 几个基本概念 视图: ...
- ASP.NET MVC显示WebForm网页或UserControl控件
ASP.NET MVC显示WebForm网页或UserControl控件 学习与使用ASP.NET MVC这样久,还是对asp.net念念不忘.能否在asp.net mvc去显示aspx或是user ...
- iOS基础 - 相片浏览器
一.需求分析 点击照片从当前照片位置动画弹出新的视图控制器显示选中的照片,新的视图控制器为全屏显示,背景为黑色,再次点击照片动画缩小至当前选中的照片位置,双击放大照片,如果已经放大则缩小,在新的视图控 ...