Android中两个Activity之间简单通信

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

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

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

Test类:

 1 package com.example.testsend;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 
 8 public class Test extends AppCompatActivity {
 9 
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_test);
14 
15         findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
16             @Override
17             public void onClick(View view) {
18                 Intent intent=new Intent(Test.this,Target.class);
19                 intent.putExtra("data","hi");
20                 startActivity(intent);
21             }
22         });
23 
24     }
25 }

activity_test.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.testsend.Test">
 8 
 9     <Button
10         android:id="@+id/button"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="Send"
14         app:layout_constraintBottom_toBottomOf="parent"
15         app:layout_constraintLeft_toLeftOf="parent"
16         app:layout_constraintRight_toRightOf="parent"
17         app:layout_constraintTop_toTopOf="parent" />
18 
19 </android.support.constraint.ConstraintLayout>

 

Target类:

 1 package com.example.testsend;
 2 
 3 import android.content.Intent;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.os.Bundle;
 6 import android.widget.TextView;
 7 
 8 public class Target extends AppCompatActivity {
 9     private TextView textview;
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_target);
14 
15         Intent intent =getIntent();
16         textview=(TextView)findViewById(R.id.textview);
17         textview.setText(intent.getStringExtra("data"));
18 
19     }
20 }

activity_target.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.example.testsend.Target">
 8 
 9     <TextView
10         android:id="@+id/textview"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         />
14 
15 </android.support.constraint.ConstraintLayout>

结果如图:

转载于:https://www.cnblogs.com/Dre2mspace/p/7221572.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值