当前位置: 首页 > article >正文

Intent--组件通信

组件通信1 获取子活动的返回值

创建Activity时实现自动注册!【Activity必须要注册才能使用】

默认 LinearLayout 布局,注意 xml 中约束布局的使用;

若需要更改 线性布局 只需要将标签更改为 LinearLayout 即可,记得 设置线性布局的方向orientation;

 activity_main1中约束布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/btn_login"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        app:layout_constraintTop_toBottomOf="@+id/btn_login">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:" />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/edt_name"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
activity_main2中约束布局

父布局约束_LineayLayout(id值是linear)

app:layout_constraintBottom_toBottomOf="parent" 底部约束
app:layout_constraintEnd_toEndOf="parent" 右边约束
app:layout_constraintStart_toStartOf="parent" 左边约束
app:layout_constraintTop_toTopOf="parent" 顶部约束

子空间约束_Button

app:layout_constraintTop_toBottomOf="@+id/linear" 顶部添加约束到父布局控件的底部
app:layout_constraintEnd_toEndOf="@+id/linear" 右边添加约束到父布局控件右边
app:layout_constraintStart_toStartOf="@+id/linear" 左边添加约束到父布局控件左边

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity2">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:" />

        <EditText
            android:id="@+id/edt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_return"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"


        android:gravity="center"
        android:text="返回"

        app:layout_constraintTop_toBottomOf="@+id/linear"
        app:layout_constraintEnd_toEndOf="@+id/linear"
        app:layout_constraintStart_toStartOf="@+id/linear" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.exp4_2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    Button btn_login;
    EditText edt_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        btn_login = findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显式启动
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                //以sub_activity的方式启动子Activity
                //参数两个 intent int(请求码)
                startActivityForResult(intent,0);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        edt_name = findViewById(R.id.edt_name);
        //得到返回值
        edt_name.setText(data.getStringExtra("name"));
    }
}
 MainActivity2.java
package com.example.exp4_2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Button btn_return;
        EditText edt_name;

        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main2);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        btn_return = findViewById(R.id.btn_return);
        edt_name = findViewById(R.id.edt_name);
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("name",edt_name.getText().toString());
                //将intent容器中储存的Activity的返回值作为参数传给setResult方法;
                setResult(0,intent);
                finish();
            }
        });
    }
}
结果 

点击 登录 按钮进入子活动2,在子活动2中点击 返回 按钮,将活动2页面中的用户名作为子活动的返回信息通过 intent 传递给父活动1,并显示在父活动1中的EditText;   

 

 

组件通信2 

题目要求 

用户点击“启动Activity1”和“启动Activity2”按钮时,程序将分别启动子SubActivity1和SubActivity2;

SubActivity1提供了一个输入框,以及“接受”和“撤销”两个按钮。如果在输入框中输入信息后点击“接受”按钮,程序会把输入框中的信息传递给其父Activity,并在父Activity的界面上显示。 如果用户点击“撤销”按钮,则程序不会向父Activity传递任何信息。 SubActivity2主要是为了说明如何在父Activity中处理多个子Activity,因此仅提供了用于关闭SubActivity2的“关闭”按钮。 

注意:在启动 子Activity 时用的是显式启动;和隐式启动的区别在于 Intent 对象的构造形式不同;

在两个 子Activity 的时候 Requestcode 和 Resultcode 的重要性;

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动Activity1"
        android:id="@+id/btn_1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动Activity2"
        android:id="@+id/btn_2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/txt_result"/>
</LinearLayout>
activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sub_activity1"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edt_input"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="接受"
        android:id="@+id/btn_accept"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="撤销"
        android:id="@+id/btn_conceal"/>
</LinearLayout>
activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity3">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="sub_activity2"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭"
        android:id="@+id/btn_close"/>
</LinearLayout>
MainActivity.java
package com.example.exp4_3;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    Button btn_1,btn_2;
    TextView txt_result;
    //必须设置成final 否则switch case 报错
    static final int request_flag1 = 1;
    static final int request_flag2 = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        btn_1 = findViewById(R.id.btn_1);
        btn_2 = findViewById(R.id.btn_2);

        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MainActivity2.class);
                //参数两个 Intent 请求码requestcode-int
                startActivityForResult(intent,request_flag1);
            }
        });

        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, MainActivity3.class);
                //参数两个 Intent 请求码requestcode-int
                startActivityForResult(intent,request_flag2);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        txt_result = findViewById(R.id.txt_result);
        switch(requestCode){
            case request_flag1:
                //判断结果码是 接受 还是 撤销
                if(resultCode==RESULT_OK){
                    txt_result.setText(data.getStringExtra("name"));
                }else{
                    txt_result.setText("sub_activity1返回空信息");
                }
                break;
            case request_flag2:
                txt_result.setText("sub_activity2返回空信息");
                break;
        }
    }
}
MainActivity2.java 
package com.example.exp4_3;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity {
    Button btn_accept,btn_conceal;
    EditText edt_input;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main2);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        btn_accept = findViewById(R.id.btn_accept);
        btn_conceal = findViewById(R.id.btn_conceal);
        edt_input = findViewById(R.id.edt_input);
        btn_accept.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //显式启动
                Intent intent = new Intent();
                intent.putExtra("name",edt_input.getText().toString());
                //参数两个 结果码Result_OK Intent
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        btn_conceal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Intent intent = new Intent();
                //setResult(RESULT_CANCELED,intent);

                //参数两个 结果码Result_CANCELED NULL
                setResult(RESULT_CANCELED,null);
                finish();
            }
        });

    }
}
MainActivity3.java
package com.example.exp4_3;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity3 extends AppCompatActivity {
    Button btn_close;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main3);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Button btn_close = findViewById(R.id.btn_close);
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(RESULT_CANCELED,null);
                finish();
            }
        });
    }
}
结果 

 

点击 启动Activity1 输入“请输入...”

 

点击 接受 

 

点击 撤销 

 

点击 启动Activity2 点击 关闭  

 

 点击 关闭 

 


http://www.kler.cn/a/452764.html

相关文章:

  • 人工智能的视觉天赋:一文读懂卷积神经网络
  • Unity3D仿星露谷物语开发8之角色移动
  • 【C语言】深入探讨 C 语言 `int` 类型大小及其跨平台影响
  • Day7补代码随想录 454.四数相加II 383赎金信 15.三数之和 18.四数之和
  • CH32V307VCT6---工程template创建
  • CI/CD是什么?
  • 华为认证考试模拟题测试题库(含答案解析)
  • STM32-笔记10-手写延时函数(SysTick)
  • nacos-服务发现注册
  • 【Linux】shell脚本:查找可执行文件和批量创建多个账户
  • LabVIEW实现NB-IoT通信
  • Pillow库
  • arXiv-2024 | STMR:语义拓扑度量表示引导的大模型推理无人机视觉语言导航
  • Vuex 的使用和原理详解
  • android 手工签名,(电子签名)
  • windows C#-编写复制构造函数
  • 掌握Go语言:配置环境变量、深入理解GOPATH和GOROOT(1)
  • Java中String类型的字符串转换成JSON对象和JSON字符串
  • [STM32] 串口通信 (十一)
  • 【落羽的落羽 C语言篇】数据存储简介
  • 车载网关性能 --- 缓存buffer划分要求
  • 109.【C语言】数据结构之求二叉树的高度
  • 探究人工智能在教育领域的应用——以大语言模型为例
  • 【JAVA高级篇教学】第五篇:OpenFeign 微服务调用注意事项
  • docker commit生成的镜像瘦身
  • 参数名在不同的SpringBoot版本中,处理方案不同