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"
tools:context=".MainActivity" > <EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="请输入账号 "/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="请输入密码 "/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/sex"
android:orientation="horizontal">
<RadioButton
android:id="@+id/sex_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/sex_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textSize="15sp"
/>
</RadioGroup>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种传值方式:传递基本数据类型"
android:onClick="clickLogin"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_login1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种传递方式:使用bundle传递基本数据"
android:onClick="clickLogin"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_login2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第三种传递方式:通过可序列化对象传值"
android:onClick="clickLogin"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_login3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第四种传递方式:通过bundle传递可序列化对象"
android:onClick="clickLogin"
android:textSize="20sp"/>
</LinearLayout>

MainActivity.java

package com.example.day05_intent_passvalue;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup; public class MainActivity extends Activity {
private EditText et_name;
private EditText et_psd;
private RadioGroup sex;
private RadioButton sex_man;
private RadioButton sex_woman;
private Button btn_login;
private Button btn_login1;
private Button btn_login2;
private Button btn_login3;
private String name;
private String psd;
private int checkedRadioButtonId;
private int sex1; /**
* Intent传值:
* 将Main1中输入的值传递到main2
*
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView(); } private void initView() {
et_name = (EditText) findViewById(R.id.et_account);
et_psd = (EditText) findViewById(R.id.et_password);
sex = (RadioGroup) findViewById(R.id.sex);
sex_man = (RadioButton) findViewById(R.id.sex_man);
sex_woman = (RadioButton) findViewById(R.id.sex_woman);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login1 = (Button) findViewById(R.id.btn_login1);
btn_login2 = (Button) findViewById(R.id.btn_login2);
btn_login3 = (Button) findViewById(R.id.btn_login3);
}
public void clickLogin(View v){
switch (v.getId()) {
case R.id.btn_login:
//传递基本数据类型
passBaseDate();
break;
case R.id.btn_login1:
//通过Bundle传递基本数据
passBaseDataByBundle();
break;
case R.id.btn_login2:
//传递可序列化对象
passSerializedObject();
break;
case R.id.btn_login3:
//通过Bundle传递可序列化对象
passSerializedObjectByBundle();
break; } }
private void passSerializedObjectByBundle() {
//获得数据
getData();
//创建Student对象,将数据封装成Student对象
Student s = new Student(name,psd,sex1);
//创建一个Intent对象,设置跳转的activity
Intent intent = new Intent(MainActivity.this,Activity2.class);
//创建Bundle对象,将序列化对象设置到bundle对象中
Bundle bundle = new Bundle();
bundle.putSerializable("Student", s);
//将bundle设置到intent对象中
intent.putExtra("BUNDLE",bundle);
//开启另一个activity
startActivity(intent); } private void passSerializedObject() {
// TODO Auto-generated method stub
getData();
Intent intent = new Intent(MainActivity.this,Activity2.class);
Student s = new Student(name,psd,sex1);
intent.putExtra("Student", s);
startActivity(intent);
} private void passBaseDataByBundle() {
// TODO Auto-generated method stub
//获得数据
getData();
//创建一个Intent对象,并设置其跳转的activity
Intent intent = new Intent(MainActivity.this,Activity2.class);
//创建Bundle对象,将值存入到Bundle对象中
Bundle extras = new Bundle();
extras.putString("NAME", name);
extras.putString("PSD", psd);
extras.putInt("SEX", sex1);
//将bundle设置到intent中
intent.putExtra("BUNDLE",extras);
//开启另一个activity
startActivity(intent);
} private void passBaseDate() {
// TODO Auto-generated method stub
//获得数据
//通过intent意图将数据传递过去
getData();
//1.创建intent意图
Intent intent = new Intent();
//2.指定需要开启的activity
intent.setClass(MainActivity.this,Activity2.class);
//3.设置需要传递的数据
intent.putExtra("NAME", name);
intent.putExtra("PSD", psd);
intent.putExtra("SEX", sex1);
//4开启另一个activity
startActivity(intent);
} private void getData() {
name = et_name.getText().toString().trim();
psd = et_psd.getText().toString().trim();
sex1 = -1;
checkedRadioButtonId = sex.getCheckedRadioButtonId();
//利用安卓自带文本工具类判断文本数据是否为空
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(psd)){
return;
}
if(checkedRadioButtonId==sex_man.getId()){
sex1 = 1;
}else{
sex1 = 2;
}
} }

Activity2.java

package com.example.day05_intent_passvalue;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView; public class Activity2 extends Activity{
private TextView tv;
private String sex;
private String name;
private String psd;
private int sexId;
private String data; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
tv = (TextView) findViewById(R.id.name);
//data = getBaseDate();
//通过Bundle对象得到基本数据类型的数据
//data =getBaseDataByBundle();
//通过序列化对象得到数据
// data = getSerializedObject();
//通过Bundle对象得到序列化对象从而得到基本类型数据
data = getSerialzedObjectByBundle();
//设置文本显示的内容
tv.setText(data); } private String getSerialzedObjectByBundle() {
//得到传过来的intent对象
Intent intent = getIntent();
//得到Bundle对象
Bundle bundle = intent.getBundleExtra("BUNDLE");
//通过bundle对象得到序列化对象
Student s = (Student) bundle.getSerializable("Student");
name = s.getName();
psd = s.getPsd();
sexId = s.getSex();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(psd)){
return null;
}
if(sexId==1){
sex = "男";
}else{
sex = "女";
}
return "账号:"+name+",密码:"+psd+",性别:"+sex;
} private String getSerializedObject() {
// TODO Auto-generated method stub
Intent intent = getIntent();
Student s = (Student) intent.getSerializableExtra("Student");
name = s.getName();
psd = s.getPsd();
sexId = s.getSex();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(psd)){
return null;
}
if(sexId==1){
sex = "男";
}else{
sex = "女";
}
return "账号:"+name+",密码:"+psd+",性别:"+sex;
} private String getBaseDataByBundle() {
//得到传过来的Intent
Intent intent = getIntent();
//获得Bundle数据
Bundle extras = intent.getBundleExtra("BUNDLE");
//通过Bundle对象得到基本类型数据
name = extras.getString("NAME");
psd = extras.getString("PSD");
sexId = extras.getInt("SEX");
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(psd)){
return null;
}
if(sexId==1){
sex = "男";
}else{
sex = "女";
}
return "账号:"+name+",密码:"+psd+",性别:"+sex; } private String getBaseDate() {
//1.获得传过来的Intent
Intent intent = getIntent();
name = intent.getStringExtra("NAME");
psd = intent.getStringExtra("PSD");
sexId = intent.getIntExtra("SEX", -1);
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(psd)){
return null;
}
if(sexId==1){
sex = "男";
}else{
sex = "女";
}
return "账号:"+name+",密码:"+psd+",性别:"+sex;
}
}

Android_Intent_passValue(4)的更多相关文章

随机推荐

  1. memcache redundancy机制分析及思考

    设计和开发可以掌控客户端的分布式服务端程序是件幸事,可以把很多事情交给客户端来做,而且可以做的很优雅.角色决定命运,在互联网架构中,web server必须冲锋在前,注定要在多浏览器版本以及协议兼容性 ...

  2. 转换时间为 “XX分钟之前”

    public static string getTimeAgo(string strDate) { string strTime = string.Empty; if (clsCommon.IsDat ...

  3. the type initializer for '' threw an exception

    the type initializer for '' threw an exception 问题:程序启动时初始化主窗口类时,弹出该错误.调查:查看类的构造函数是否会有异常抛出.解决:去掉类的构造函 ...

  4. python 中 struct 用法

    下面就介绍这个模块中的几个方法. struct.pack():我的理解是,python利用 struct模块将字符(比如说 int,long ,unsized int 等)拆成 字节流(用十六进制表示 ...

  5. microsoft的罗马帝国——浪潮之巅

    其实开始读微软的这篇已经比较久了,从来学校的前一天晚上等车的时候就开始读了,直到今天才看完.嗯,微软的确是个帝国. 那就从头开始讲把,关于帝国的传奇都是比较长的故事呢.至于我的叙述水平和我的知识水平都 ...

  6. adb logcat调试中常用的命令介绍

    Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命 令来查看和使用. adb logcat 命令格式 : ad ...

  7. Windows下ffmpeg的完美编译

    纠结了好几天,终于搞定了,小结一下. 1.下载ffmpeg源码,官网 2.编译环境Msys的安装配置,http://blog.csdn.net/jszj/article/details/4028716 ...

  8. DOS命令关闭进程

    1.开始-运行,输入cmd后回车; 2.在DOS提示符下,先用命令 tasklist 回车来获取进程的 PID(例如获取了Explorer.EXE进程的PID为1988); 3.然后再输入命令:tas ...

  9. HDU-4705 Y 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4705 题意:给一颗树,从树上任意选择3个点{A,B,C},要求他们不在一条链上,求总共的数目. 容易想 ...

  10. 你所不知道的五件事情--java.util.concurrent(第二部分)

    这是Ted Neward在IBM developerWorks中5 things系列文章中的一篇,仍然讲述了关于Java并发集合API的一些应用窍门,值得大家学习.(2010.06.17最后更新) 摘 ...