-------siwuxie095

传递简单数据

(1)首先创建一个项目:SendArgs

(2)选择API:21 Android 5.0

(3)选择
Empty Activity

(4)默认

(5)完成,一览:

(6)先进
activity_main.xml 里的 Text 手动添加一个Button,

删掉自带的TextView,如下:

<?xml
version="1.0"
encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.siwuxie095.sendargs.MainActivity">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/btnStartAty"

android:text="启动另一个Activity"/>

</RelativeLayout>

(7)new 一个
Empty Activity:TheAty

(8)给activity_the_aty.xml添加一个TextView,如下:

<?xml
version="1.0"
encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_the_aty"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="com.siwuxie095.sendargs.TheAty">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World"

android:id="@+id/tv"/>

</RelativeLayout>

(9)在MainActivity.java 中添加:findViewById(R.id.btnStartAty)

和setOnClickListener(new OnClick…),会自动生成代码,接着在onClick()

函数里创建一个Intent,通过 Intent 的 putExtra() 传参,最后通过 startActivity()

把Intent对象传入:

package com.siwuxie095.sendargs;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override

protected
void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() {

@Override

public
void onClick(View v) {

//创建一个new Intent()实例,传入Context和Class类型的参数

//对应MainActivity.this 和
被启动的Activity的类定义 TheAty.class

Intent i = new Intent(MainActivity.this,TheAty.class);

//通过Intent对象,调用putExtra()方法,传参

//这里传入String和String类型的参数,实际上就是键值对

i.putExtra("data","你好
我是传递的参数");

//传入i

startActivity(i);

}

});

}

}

(10)在 TheAty.java 中获取数据,如下:

package com.siwuxie095.sendargs;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.TextView;

public class TheAty extends AppCompatActivity {

private TextView tv;

@Override

protected
void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_the_aty);

//TheAty是通过MainActivity中的Intent启动,直接通过getIntent()方法

//获取与这个Activity相关的Intent

Intent i=getIntent();

//要访问TextView,先在上面声明

//findViewById()的返回值在编译时只能查看到类型是View,而事实上是TextView类型

//需要强制类型转换

tv= (TextView) findViewById(R.id.tv);

//调用setText()方法设置字符串,这里字符串通过Intent获取

tv.setText(i.getStringExtra("data"));

}

}

(11)发送到手机,运行一览:

【made by siwuxie095】

Activity之间传递参数(一)的更多相关文章

  1. Android学习总结——Activity之间传递参数

    核心内容:一.在 Activity 之间传递简单数据二.在 Activity 之间传递复杂数据 三.在 Activity 之间传递自定义值对象   软件环境:Android Studio   一.在 ...

  2. Activity之间传递参数(四)

    --------siwuxie095 获取Activity的返回参数 1.首先修改两个布局文件,都修改为 LinearLayout 布局, 添加orientation属性为:vertical. (1) ...

  3. 在Activity之间传递参数(四)

    获取Activity的返回参数(在参数(三)User的例子的基础上实现): 1.activity_the_aty.xml文件:<EditText android:id="@+id/ed ...

  4. Activity之间传递参数(三)

    ------siwuxie095 传递值对象,即自定义的有数据类型的对象 1.首先 new 一个 class:User,用于创建自定义对象,同时右键 Generate 出 Constructor.se ...

  5. Activity之间传递参数(二)

    ------siwuxie095 传递数据包 1.传递数据包要用到Bundle,MainActivity.java中: package com.siwuxie095.sendargs; import ...

  6. 在Activity之间传递参数(一)

    准备: 一.创建主界面:activity_main.xml文件中<Button android:text="启动另一个Activity" android:id="@ ...

  7. 在Activity之间传递参数(三)——serializable和parcelable的区别

    传递值对象: 一.serializable实现:简单易用 serializable的迷人之处在于你只需要对某个类以及它的属性实现Serializable 接口即可.Serializable 接口是一种 ...

  8. 在Activity之间传递参数(二)

    传递数据包bundle: 1.MainActivity.class: findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClic ...

  9. [Android学习]Activity之间传递对象和对象集合

    开发过程中,Activity之间传递数据是必不可少的,android中使用Intent和Bundle作为数据载体,在Activity之间传递,对于基础数据类型,Bundle已经提供相关的put,get ...

随机推荐

  1. 封装tip控件

    在界面上有时需要显示一个提示,大多的前端框架都把提示做成一个带有小尖角的提示框,因此自己也仿照了实现了一下,效果图如下: 尖角的实现很简单的,一般都是通过css将div的宽高设置为0,然后将尖角朝向的 ...

  2. python download

    今天下载 python3 , 从官网下 速度 平均 十几K,网上 搜了下.提供的下载地址 几乎都是 官网的. 于是 下了个 百度同步盘,做 公开分享. 提供给 大家下载,速度 有 300 多K,提高了 ...

  3. 2、SIP

    1.初学者笔记:http://www.cnblogs.com/gnuhpc/archive/2012/01/16/2323637.html 2.SIP头字段解释:http://www.cnblogs. ...

  4. 批量删除wps文档里的回车符的方法!WPS使用技巧分享!

    有时候整理文档的时候,如果是从网上复制的文字,可能会因为复制而产生很多的回车符.怎样能批量去掉这些个回车符呢,下面马上告诉你批量删除wps文档里的回车符的方法!WPS使用技巧分享! 想要批量删除批量删 ...

  5. VS CODE 下的 ESLint 安装以及使用

    经过半年的前端磨练(就是不停地敲敲代码),自认水平提高的速度还是可以的. 现在回头看下写过的代码,发现以前写的代码真的是不忍惨睹. 比如 变量名乱起 风格多变 注释乱写或者没写 等等错误; 这不是一个 ...

  6. 通过sqoop来传输mysql/oracle/vertica数据至HBASE

    首先要注意将连接用的jar包,放到sqoop目录下,我的是/var/lib/sqoop 如果没有主键,则要加上-m 1 export正确的jdk目录 当做key的列必须唯一存在,不然报错 --mysq ...

  7. phpstorm 16.1 注册码

    phpstorm 2016.1 的注册与phpstorm 10 相同,可以采用:“服务器注册” 方式进行注册,又快又方便. 服务器注册:http://idea.qinxi1992.cn IDEA 20 ...

  8. java自定义异常(Exception、throws、try-catch)

    一.What is ... 异常处理就是容错处理机制.通过构造一个陷阱来捕获运行时的可预见错误,经对该错误进行适当处理后,让程序能继续运行不至于崩溃. 二.Who will ... 异常由系统环境引发 ...

  9. Visual Studio 2013 always switches source control plugin to Git and disconnect TFS

      A few days ago, I've been facing a strange behavior with Visual Studio 2013.   No matter what solu ...

  10. mobx源码解读3

    计算属性 function Todo() { this.id = Math.random() mobx.extendObservable(this, { aaa: 222, bbb: 11, ccc: ...