安卓--使用Intent实现Activity之间传值与跳转
http://blog.csdn.net/cjjky/article/details/6337447
在一个Android的应用程序中,很少只存在一个Activity,一般都有多个Activity,如何在多个Activity之间进行跳转和传值呢?我们可以通过 Intent 对象来实现上面所述的功能。本例通过在FirstActivity中实现两个数进行相加,把相加的结果值通过Intent把值传递到SecondActivity中。
我们新建一个Android的工程,有两个Activity,分别为FirstActivity 和 SecondActivity ,同时在资源文件下的 res/layout 目录下也有两个布局文件,分别为 main.xml【对应FirstActivity】和secondlayout.xml【对应SecondActivity】,目录的结构图如下:

在 main.xml 布局文件中添加两个EditText控件,一个TextView控件,一个Button按钮,代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <EditText
- android:id="@+id/firstNum"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="加"
- android:textSize="20sp"
- />
- <EditText
- android:id="@+id/secondNum"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <Button
- android:id="@+id/btnCalc"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="计算结果"/>
- </LinearLayout>
在secondlayout.xml布局文件中,添加一个EditText控件来显示从FirstActivity中传过来的值。代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <EditText
- android:id="@+id/result"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
在FirstActivity中的代码如下:
- package com.andyidea.test;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class FirstActivity extends Activity {
- EditText firstNum,secondNum;
- Button calc;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- firstNum = (EditText)findViewById(R.id.firstNum);
- secondNum = (EditText)findViewById(R.id.secondNum);
- calc = (Button)findViewById(R.id.btnCalc);
- calc.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String num1 = firstNum.getText().toString();
- String num2 = secondNum.getText().toString();
- Intent intent = new Intent();
- intent.putExtra("one", num1);
- intent.putExtra("two", num2);
- intent.setClass(FirstActivity.this, SecondActivity.class);
- startActivity(intent);
- }
- });
- }
- }
在SecondActivity中的代码如下:
- package com.andyidea.test;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.EditText;
- public class SecondActivity extends Activity {
- EditText result;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.secondlayout);
- result = (EditText)findViewById(R.id.result);
- Intent intent = getIntent();
- String num1 = intent.getStringExtra("one");
- String num2 = intent.getStringExtra("two");
- int ret = Integer.parseInt(num1) + Integer.parseInt(num2);
- result.setText(ret+"");
- }
- }
同时别忘了在Manifest配置文件中对SecondActivity进行注册。
安卓--使用Intent实现Activity之间传值与跳转的更多相关文章
- 使用Intent实现Activity之间传值与跳转(转)
转:http://blog.csdn.net/cjjky/article/details/6337447 在一个Android的应用程序中,很少只存在一个Activity,一般都有多个Activity ...
- Intent在Activity之间传值的几种方式
发这篇博客主要讲一下Android中Intent中如何传值的几种方法: 1:基本数据类型,包含了Java八种基本数据类型和CharSequece文本2:八种数据类新对应数组和CharSequece文本 ...
- 安卓开发-intent在Activity之间数据传递
安卓开发-intent在Activity之间数据传递 [TOC] intent实现普通跳转 使用intent的setclass方法,示例(由此界面跳转到NewActivity界面) //使用setOn ...
- Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值
原文:[置顶] Android菜鸟的成长笔记(10)——使用Bundle在Activity之间传值 前面我们了解了如何启动一个Activity,一个Activity在启动另外一个Activity的时候 ...
- 【Android基础】利用Intent在Activity之间传递数据
前言: 上一篇文章给大家聊了Intent的用法,如何用Intent启动Activity和隐式Intent,这一篇文章给大家聊聊如何利用Intent在Activity之间进行沟通. 从一个Activ ...
- 从0系统学Android-2.3使用 Intent 在 Activity 之间穿梭
2.3 使用 Intent 在 Activity 之间穿梭 在上一节中我们已经学会了如何创建一个 Activity 了.对于一个应用程序来说,肯定不可能只有一个 Activity.下面就来学习多个 A ...
- intent实现Activity之间跳转的各种传值
一.在Activity之间传递String类型的数据 传递 @Override public void onClick(View v) { String num1 = firstNum.getText ...
- Android中如何使用Intent在Activity之间传递对象[使用Serializable或者Parcelable]
http://blog.csdn.net/cjjky/article/details/6441104 在Android中的不同Activity之间传递对象,我们可以考虑采用Bundle.putSeri ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
随机推荐
- php 判断字符串在另一个字符串中位置
$email='user@example.com'; //定义字符串$result=strstr($email,'@'); //返回子字符串echo $result; / ...
- iis 下的 selfssl
当然,如果你想省掉所有这些麻烦也行,最简单的在IIS启动SSL的方法只要3步: 1. 下载 IIS 6.0 Resource Kit Tools: http://www.microsoft.com/d ...
- jquery 清空表达内容
function clearForm(objE) { $(objE).find(':input').each( function() { switch (this.type) { case 'pass ...
- (转)MapReduce中的两表join几种方案简介
转自:http://blog.csdn.net/leoleocmm/article/details/8602081 1. 概述 在传统数据库(如:MYSQL)中,JOIN操作是非常常见且非常耗时的.而 ...
- C#中DataTable与实体集合通用转换(使用扩展方法)
本案例提供了:把DataRow转换为单个实体.dataTable转换为List泛型支持时间格式转换. 下文的方法都是扩展方法.扩展方法要求写在静态类中,方法也要静态. 它必须在一个非嵌套.非泛型的静态 ...
- 问题 K: 【USACO2012Feb】植草 {Bronze题2}
按着矩形周长的思路,到当前边的时候,前一层的覆盖数乘以高度加入 ans 就行,然而真正的算法可能并不是这个..只能想到这个了 ; type node=record l,r,mid,sum,delta: ...
- Configuring My Site in SharePoint 2010
Configuring the User Profile Service in SharePoint 2010 http://sharepointgeorge.com/2010/configuring ...
- SQL SERVER 强制排序规则查询
有时会需要在2个DB之间的数据做比较, 但因为一些原因, 数据库的默认排序规则是不一样的, 例如 SELECT A.Col1, B.Col1, A.* FROM DB1.dbo.A LEFT JOIN ...
- Ubuntu下安装eclipse及PyDev插件注意事项
一.安装eclipse前一定要先安装jdk 1.到http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880 ...
- arm-none-eabi-gcc,makefile,stm官方库构建stm32f4xx工程
参考文章:http://www.stmcu.org/module/forum/forum.php?mod=viewthread&tid=603753&highlight=ubuntu ...