首先是布局文件,如下:

activity_main.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="horizontal">
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"
/>
<!--定义onclick函数-->
</LinearLayout>
接下来是主Activity,MainActivity.java

package com.example.administrator.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
//发送Intent对应字符串内容的key
public static final String Intent_key="MESSAGE";
//EditText
private EditText editText =null;
private void initView(){
editText= (EditText) findViewById(R.id.edit_message);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置布局
setContentView(R.layout.activity_main);
initView();
}
//发送消息,启动secondActivity!
public void sendMessage(View view){
Intent intent = new Intent(this,secondActivity.class);
String text =editText.getText().toString();
intent.putExtra(Intent_key,text);
startActivity(intent);
}
}
    请注意,在button的点击事件中,首先新建Intent对象,指明activity跳转的来源与去向,然后通过Intent对象的putExtra()方法,传入editText获取的文本。注意Intent在传递消息时,需要通过key来标识消息。所以我们将Intent_key 定义为:public  static  final String Intent_key="MESSAGE";
    最后是第二个Activity, secondActivity.java

package com.example.administrator.helloworld;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class secondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent =getIntent();
String text =intent.getStringExtra(MainActivity.Intent_key);
TextView textView =new TextView(this);
textView.setTextSize(40);
textView.setText(text);
// Set the text view as the activity layout
setContentView(textView);
}
}
    在onCreate()方法中,首先获取Intent对象,通过key获取通过Intent传递的message。同时新建TextView对象,设置其显示的文本为message。并设置TextView作为该activity的布局。
    以下是程序运行截图:
                       
 

android入门,activity跳转,并传递message的更多相关文章

  1. Android的Activity跳转动画各种效果整理

    Android的Activity跳转就是很生硬的切换界面.其实Android的Activity跳转可以设置各种动画,本文整理了一些,还有很多动画效果,就要靠我们发挥自己的想象力 大家使用Android ...

  2. Android之Activity跳转

    简述 如果把每个activity看成一个页面的话,那么activity之间的跳转和页面的之间的跳转基本上是一样的.首先需要监听一个事件,当这个事件发生的时候,就进行跳转.html中有个<a sr ...

  3. Android——关于Activity跳转的返回(无返回值和有返回值)——有返回值

    说明: 跳转页面,并将第一页的Edittext输入的数据通过按钮Button传到第二页用Edittext显示,点击第二页的 返回按钮Button返回第一页(改变第二页的Edittext的内容会传至第一 ...

  4. android 15 activity跳转

    从一个屏幕跳到另一个屏幕,一个activity跳转到另一个activity,Intent类用于组件之间传递数据和跳转,组件包括不仅activity. package com.sxt.day04_01; ...

  5. android入门——Activity(2)

    主要内容:一.IntentFlag  二.简单复杂数据传递  三.数据回传  四.打开系统界面  五.IntentFilter匹配 一.IntentFlag 复制一段内容    来源 http://i ...

  6. android单元测试 activity跳转 以及 input 输入后 测试

    Android junit实现多个Activity跳转测试 分类: Android Junit测试2011-11-14 16:49 1601人阅读 评论(2) 收藏 举报 androidjunitla ...

  7. Android笔记——Activity中的数据传递案例(用户注冊)

    1.创建程序activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  8. android入门——Activity(1)

    结构图 mainfests文件夹下面有一个AndroidMainfest.xml文件,类似web开发中的web.xml文件负责整个项目的配置,每当我们新建一个activity,都要在这个文件中进行配置 ...

  9. Activity跳转时传递Bitmap对象的实现

    前言 相信大家可能都了解Activity跳转时我们是能够传递參数的,比方使用Intent传递或者Bundle来传递,把当前Activity的一些信息传递给将要跳转到的新的Activity.可是不知道大 ...

随机推荐

  1. MOSFET与MOSFET驱动电路原理及应用(转)

    源:http://www.micro-bridge.com/news/news.asp?id=258 在使用MOS管设计开关电源或者马达驱动电路的时候,大部分人都会考虑MOS的导通电阻,最大电压等,最 ...

  2. zf-关于通知公告如果发布的是无限制时间的,那么默认隐藏时间输入框的问题

    function initElements(network){ var nonoticeLimit = document.getElementById("nonoticeLimit" ...

  3. '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "AttentionController" nib but the view outlet was not set.'

    问题出现原因: 先创建控制器的.h与.m文件,后来创建了相应的xib文件,运行后出现这个问题. 解决办法:

  4. j2ee常用包的作用

    1.antlr-2.7.7.jar 呵呵 一句话,没有此包,hibernate不会执行hql语句   2.aopalliance-1.0.jar 这个包是AOP联盟的API包,里面包含了针对面向切面的 ...

  5. PAT (Advanced Level) 1058. A+B in Hogwarts (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  6. 用JAVA捕获屏幕、屏幕录像、播放

    http://blog.csdn.net/njchenyi/article/details/447554 用JAVA捕获屏幕.屏幕录像.播放 标签: javaexceptionimageimportn ...

  7. springMVC文件上传优化

    1. 新建web project 2. 填 jar,注意新加入两个上传文件的jar, commons-io, commons-fileupload 3. 改写web.xml <?xml vers ...

  8. 搭建git服务器及利用git hook自动布署代码

    注意:服务器:Ubuntu Server 14.04,我的项目比较小,所有操作都使用root,建议最好新建一个用户(需要有管理员权限,否则在后面使用hooks自动部署代码时会出现各种权限问题,很蛋疼的 ...

  9. CDOJ 1273 God Qing's circuital law

    暴力枚举+idea.做的时候mod写错了,写成了1000000009,找了两个多小时才发现...... a[1],a[2],a[3]....a[N] b[1],b[2],b[3]....b[N] 首先 ...

  10. Content Provider 小结

    Android中的四大组件之一ContentProvider,它支持多个应用间进行存储和读取数据等操作,实现不同应用间的数据共享. ContentProvider,解释为内容提供商.顾名思义,就是对外 ...