Android 在不同Actitity之间数据传递
本文实现一个简易的人品计算器来实践在不同Actitity之间数据传递
intent的数据传递
从A界面打开B界面 把A界面的数据传递给B界面
1. intent.setData(uri) -- intent.getData();
可以传递简单的文本数据。
2. intent.putExtra(name, value)
8大基本类型的数据,数组都可以传递
String对象 可以传递 charSequence
可以序列化的对象(序列化到文件) Serializable 也可以传递
可以序列化的对象(序列化到内存) Parcelable 也可以传递 bitmap
可以传递 一个map集合 Bundle extras = new Bundle();
本文地址:http://www.cnblogs.com/wuyudong/p/5679191.html,转载请注明源地址。
新建项目,MainActivity中的代码如下:
package com.wuyudong.testrp; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText et_name; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText)findViewById(R.id.et_name);
} public void click(View view) {
String name = et_name.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "名字不能为空", 1).show();
return;
}
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("name", name);
startActivity(intent);
} }
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"
tools:context=".MainActivity" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="人品计算器"
android:textColor="#ff0000"
android:textSize="26sp" /> <EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" >
</EditText> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:layout_centerInParent="true"
android:onClick="click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
>
</Button>
</RelativeLayout> </LinearLayout>
界面如下:

输入姓名后,点击按钮跳转到另一个结果界面:activity_result.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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试结果:"
android:textColor="#ff0000"
android:textSize="20sp" /> <TextView
android:id="@+id/tv_result"
android:text="xxx:"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#99000000"
android:textSize="17sp" /> <ProgressBar
android:id="@+id/progressBar1"
android:max="100"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" /> </LinearLayout>
界面如下:

ResultActivity.java代码如下:
package com.wuyudong.testrp; import java.util.Random; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView; public class ResultActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView tv_result = (TextView) findViewById(R.id.tv_result); Intent intent = getIntent(); String name = intent.getStringExtra("name");
Random rm = new Random();
int rp = rm.nextInt(101);
tv_result.setText(name + ":您的人品值为:" + rp); ProgressBar pb = (ProgressBar)findViewById(R.id.progressBar1);
pb.setProgress(rp); }
}
Android 在不同Actitity之间数据传递的更多相关文章
- android 应用程序Activity之间数据传递与共享的几种途径
一.基于消息的通信机制 Intent ---boudle ,extraAndroid为了屏蔽进程的概念,利用不同的组件[Activity.Service]来表示进程之间的通信!组件间通信的核心机制是I ...
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- 解析activity之间数据传递方法的详解
转自:http://www.jb51.net/article/37227.htm 本篇文章是对activity之间数据传递的方法进行了详细的分析介绍,需要的朋友参考下 1 基于消息的通信机制 ...
- PCB MVC启动顺序与各层之间数据传递对象关系
准备着手基于MVC模式写一套Web端流程指示查看,先着手开发WebAPI打通数据接口,后续可扩展手机端 这里将MVC基本关系整理如下: 一.MVC启动顺序 二.MVC各层之间数据传递对象关系
- android Activity之间数据传递 Parcelable和Serializable接口的使用
Activity之间传数据时,为了避免麻烦,往往会将一些值封装成对象,然后将整个对象传递过去.传对象的时候有两种情况,一种是实现Parcelable接口,一种是实现Serializable接口.0.解 ...
- Android activity之间数据传递和共享的方式之Application
1.基于消息的通信机制 Intent ---bundle ,extra 数据类型有限,比如遇到不可序列化的数据Bitmap,InputStream,或者LinkedList链表等等数据类型就不太好用 ...
- Android——不同activity之间数据传递
/* * 不同activity之间数据的传递 */ public class MainActivity extends Activity { private EditText et_name; @Ov ...
- 转-Activity之间数据传递之Intent数据传递
Intent意图 可用于Activity之间的数据传递,一般可分为下面两种情况,从当前Activity传递到目标Activity后有无返回值: 1.传递后无返回值的情况: 1 2 3 4 5 6 7 ...
- Android笔记——Activity中的数据传递案例(用户注冊)
1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...
随机推荐
- position属性absolute与relative 详解
最近一直在研究javascript脚本,熟悉DOM中CSS样式的各种定位属性,以前对这个属性不太了解,从网上找到两篇文章感觉讲得很透彻,收藏下来,唯恐忘记.一.解读absolute与relative ...
- Javascript 布尔操作符总结
在一门编程语言中,布尔操作符的重要性堪比相等操作符.如果没有测试两个值关系的能力,那么诸如if...else和循环之类的语句就不会有用武之地了.在像javascript这样弱类型语言更有其妙用,让我们 ...
- SQL--存储过程
声明和调用有返回值的存储过程 分页存储过程 转账的存储过程:
- 【转】 Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...
- 获取MS SQL TABLE列名列表
在MS SQL Server中,想获取表的所有列名,可以使用下面SQL语句: SELECT [COLUMN_NAME] FROM [INFORMATION_SCHEMA].[Columns] WHER ...
- Zookeeper安装,Zookeeper单机模式安装
http://zookeeper.apache.org/releases.html#download 下载解压到(我自己的)解压到 /usr/local 下 把名字改成 zookeeper 进入zoo ...
- oracle 数据类型详解---日期型(转载)
oracle 数据类型详解---日期型 oracle数据类型看起来非常简单,但用起来会发现有许多知识点,本文是我对ORACLE日期数据类型的一些整理,都是开发入门资料,与大家分享: 注:由于INTER ...
- JavaWeb前端基础复习笔记系列 一
课程:孔浩前端视频教程(CMS内容管理系统case) 1.背景知识 ASPCMS,是一个基于asp的CMS.类似于Jeecms是基于Java的.aspcms:http://www.aspcms.com ...
- 基于HTML5 geolocation 实现的天气预报功能
最近一直在学习HTML5,因为8月份要开发手机项目了.所以先把HTML5学习下. 基本思路: 1. 用户未设置任何城市之前,根据HTML5 geolocation 获取用户所在的地理位置. 2. 根据 ...
- 《深入.NET平台和C#编程》内部测试题
一 选择题 1) 以下关于序列化和反序列化的描述错误的是( C). a) 序列化是将对象的状态存储到特定存储介质中的过程 b) 二进制格式化器的Serialize()和D ...