在Android中,一个界面被称为一个activity,在两个界面之间通信,采用的是使用一个中间传话者(即Intent类)的模式,而不是直接通信。

下面演示如何实现两个activity之间的通信。

信息的发起者为Test,接收者为Target,代码如下:

Test类:

 package com.example.testsend;

 import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; public class Test extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(Test.this,Target.class);
intent.putExtra("data","hi");
startActivity(intent);
}
}); }
}

activity_test.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testsend.Test"> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>

Target类:

 package com.example.testsend;

 import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView; public class Target extends AppCompatActivity {
private TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target); Intent intent =getIntent();
textview=(TextView)findViewById(R.id.textview);
textview.setText(intent.getStringExtra("data")); }
}

activity_target.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testsend.Target"> <TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </android.support.constraint.ConstraintLayout>

结果如图:

最新文章

  1. 项目管理师prince2
  2. AngularJS in Action读书笔记1——扫平一揽子专业术语
  3. 高通Android平台硬件调试之Camera篇
  4. linux系统主要常见目录结构
  5. Linux内核--网络栈实现分析(二)--数据包的传递过程--转
  6. java学习多线程之卖票示例
  7. CSS背景颜色、背景图片、平铺、定位、固定
  8. WAMPServer配置修改及问题汇总
  9. Python:正则表达式(一):search()、match()、findall() 的区别
  10. To be better —msup荣获平安科技“2018年度优秀合作伙伴”称号
  11. LeetCode--No.011 Container With Most Water
  12. Nginx Linux安装与部署
  13. C# Winform继承窗体打开设计器白屏的一例解决方法
  14. tomcat配置问题
  15. 009.MySQL-Keepalived搭配脚本03
  16. ELMAH 使用
  17. eclipse 将javaWeb项目转化成maven项目
  18. 【微信小程序】——wxss引用外部CSS文件及iconfont
  19. Leetcode 98 验证二叉搜索树 Python实现
  20. Guava API - FluentIterable Predicate Function Odering Range Splitter

热门文章

  1. Java获取微信小程序二维码
  2. OOAD理论知识小结
  3. JSONPath使用说明
  4. [NOI 2015]寿司晚宴
  5. 呼叫WCF Service的方法出现Method not allowed异常
  6. SQL SERVER TRANSACTION 事物
  7. ActiveMQ 控制面板信息含义
  8. Hashmat the brave warrior(UVa10055)简单题
  9. POJ3436(KB11-A 最大流)
  10. Context 上下文