Android使用Bundle在Activity之间传递信息实例
2014-02-09 15:58
387 查看
实例效果
点击注册按钮之后,跳转到一个新的页面,在新的页面获取到输入的信息
首先编写注册页面
login.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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="姓名"/>
<EditText android:id="@+id/input_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="4-12位字符"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="邮箱"/>
<EditText android:id="@+id/input_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="密码"/>
<EditText android:id="@+id/input_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="6位数字密码"/>
</LinearLayout>
<Button android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="注册"/>
</LinearLayout>
LoginActivity.java
package com.hanqing.bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final EditText name=(EditText) super.findViewById(R.id.input_name);
final EditText email=(EditText) super.findViewById(R.id.input_email);
final EditText password=(EditText) super.findViewById(R.id.input_password);
Button submit=(Button) super.findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(LoginActivity.this,ReceiveDataActivity.class);
Person person=new Person();
person.setName(name.getText().toString());
person.setEmail(email.getText().toString());
person.setPassword(password.getText().toString());
Bundle data=new Bundle();
data.putSerializable("person", person);
intent.putExtras(data);
//启动Activity
startActivity(intent);
}
});
}
}
编写数据接收页面
receive_data.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/receive_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView android:id="@+id/receive_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView android:id="@+id/receive_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
</LinearLayout>
ReceiveDataActivity.java
package com.hanqing.bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveDataActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recive_data);
TextView receiveName=(TextView) super.findViewById(R.id.receive_name);
TextView receiveEmail=(TextView) super.findViewById(R.id.receive_email);
TextView receivePassword=(TextView) super.findViewById(R.id.receive_password);
//获取启动的Intent
Intent intent=getIntent();
Person person=(Person) intent.getExtras().getSerializable("person");
receiveName.setText("姓名 "+person.getName());
receiveEmail.setText("邮箱 "+person.getEmail());
receivePassword.setText("密码 "+person.getPassword());
}
}
配置AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqing.bundle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.hanqing.bundle.LoginActivity"
android:label="在Activity之间传递数据">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.hanqing.bundle.ReceiveDataActivity"
android:label="接受数据">
</activity>
</application>
</manifest>
点击注册按钮之后,跳转到一个新的页面,在新的页面获取到输入的信息
首先编写注册页面
login.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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="姓名"/>
<EditText android:id="@+id/input_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="4-12位字符"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="邮箱"/>
<EditText android:id="@+id/input_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="密码"/>
<EditText android:id="@+id/input_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:textSize="16sp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:hint="6位数字密码"/>
</LinearLayout>
<Button android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="注册"/>
</LinearLayout>
LoginActivity.java
package com.hanqing.bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final EditText name=(EditText) super.findViewById(R.id.input_name);
final EditText email=(EditText) super.findViewById(R.id.input_email);
final EditText password=(EditText) super.findViewById(R.id.input_password);
Button submit=(Button) super.findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent=new Intent(LoginActivity.this,ReceiveDataActivity.class);
Person person=new Person();
person.setName(name.getText().toString());
person.setEmail(email.getText().toString());
person.setPassword(password.getText().toString());
Bundle data=new Bundle();
data.putSerializable("person", person);
intent.putExtras(data);
//启动Activity
startActivity(intent);
}
});
}
}
编写数据接收页面
receive_data.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/receive_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView android:id="@+id/receive_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView android:id="@+id/receive_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"/>
</LinearLayout>
ReceiveDataActivity.java
package com.hanqing.bundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ReceiveDataActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recive_data);
TextView receiveName=(TextView) super.findViewById(R.id.receive_name);
TextView receiveEmail=(TextView) super.findViewById(R.id.receive_email);
TextView receivePassword=(TextView) super.findViewById(R.id.receive_password);
//获取启动的Intent
Intent intent=getIntent();
Person person=(Person) intent.getExtras().getSerializable("person");
receiveName.setText("姓名 "+person.getName());
receiveEmail.setText("邮箱 "+person.getEmail());
receivePassword.setText("密码 "+person.getPassword());
}
}
配置AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqing.bundle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="com.hanqing.bundle.LoginActivity"
android:label="在Activity之间传递数据">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.hanqing.bundle.ReceiveDataActivity"
android:label="接受数据">
</activity>
</application>
</manifest>
相关文章推荐
- Android 使用bundle在activity之间交换数据 DEMO (填写注册信息实例)
- Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用!
- Android中级教程之(五)----->不同Activity之间的数据传递,Bundle对象的使用!
- Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用!
- 使用Bundle在Activity之间传递信息
- 【Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用】的改进!
- Android判断网络状态是否断开+Android完全关闭应用程序+ 本文讲述了Android中不同Activity之间的数据传递 — Bundle对象的实现(-)+Android中Bundle的使用示例
- Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用!
- Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用!
- Android笔记三.不同Activity之间的传递数据(Bundle对象的使用)
- android bundle 在activity之间传递数据 点击一组图片放大,再次点击恢复
- Android入门篇四:使用剪切板在Activity之间传递对象数据
- 关于android中Activity之间添加信息的传递
- android之Activity之间的信息传递
- Android中使用Intent在Activity之间传递对象(使用Serializable或者Parcelable)的方法
- 使用Bundle在Activity间传递信息
- Android 使用intent.putExtra实现Activity之间的参数传递
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
- 使用bundle在Activity之间传递数据
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]