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