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 点击 关闭
点击 关闭