Android Studio基础项目-两个Activity的Intent跳转与传值,并onActivityResult回传一个/多个值,与回传消息内容。
内容如图:
代码接该章节:
一、简单传值
第一步:在MainActivty.java
第二步:重新方法
第三步:打印回传数据值信息
MainActivty.java的代码内容:
package com.xwb.userintent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.renderscript.RenderScript;
import android.util.Log;
import android.view.View;
import com.xwb.userintent.model.User;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
public void tiaozhuan(View v){
Intent intent = new Intent();//跳转意图
//告诉意图,想从哪个界面跳转到哪里
intent.setClass(MainActivity.this,MainActivity_sec.class);//跳转的源、目的
//发送数据,java基本数据类型有8类,不支持Map类型。设置完数据后,需要到MainActivity_sec.activity中接收数据
intent.putExtra("name","haha");
intent.putExtra("age",20);//int型数据
intent.putExtra("aihao",new String[]{"唱歌","打游戏"});
//使用Bundle,发送数据
Bundle bundle = new Bundle();//创建
bundle.putInt("score",100);//设置score值为100
bundle.putString("info","这里是Bundle传值");
intent.putExtras(bundle);//关键处,把bundle值传给intent
//传自定义数据,发送数据
User user = new User("lidada","123456",40);
intent.putExtra("user",user);
//startActivity(intent);//执行跳转
startActivityForResult(intent,100);//第1步设置回传值
}
//第2步重新方法
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("MainActivity","onActivityResult");//打印该方法是否回传数据值信息
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
第4步:运行APP,当中必须点击返回按钮,才能看到是否回传信息
二、通过requestCode的单一值来判断执行事件
package com.xwb.userintent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.renderscript.RenderScript;
import android.util.Log;
import android.view.View;
import com.xwb.userintent.model.User;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
public void tiaozhuan(View v){
Intent intent = new Intent();//跳转意图
//告诉意图,想从哪个界面跳转到哪里
intent.setClass(MainActivity.this,MainActivity_sec.class);//跳转的源、目的
//发送数据,java基本数据类型有8类,不支持Map类型。设置完数据后,需要到MainActivity_sec.activity中接收数据
intent.putExtra("name","haha");
intent.putExtra("age",20);//int型数据
intent.putExtra("aihao",new String[]{"唱歌","打游戏"});
//使用Bundle,发送数据
Bundle bundle = new Bundle();//创建
bundle.putInt("score",100);//设置score值为100
bundle.putString("info","这里是Bundle传值");
intent.putExtras(bundle);//关键处,把bundle值传给intent
//传自定义数据,发送数据
User user = new User("lidada","123456",40);
intent.putExtra("user",user);
//startActivity(intent);//执行跳转
startActivityForResult(intent,100);//第1步设置回传值
}
//第2步重新方法
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("MainActivity","onActivityResult");//打印该方法是否回传数据值信息
switch (requestCode){//通过requestCode值来判断执行事件
case 100:
Log.i("MainActivity",""+requestCode+"."+resultCode);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
运行app
三、通过requestCode的多个值来判断执行事件
第一步:在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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:onClick="back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"/>
<Button
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Activity的显示界面"/>
</LinearLayout>
第二步:在java代码中增加back的点击事件
package com.xwb.userintent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.xwb.userintent.model.User;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
public class MainActivity_sec extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
//接收MainActivity的传过来的数据值,并保存到相应值中
Intent intent = getIntent();
int age = intent.getIntExtra("age",0);//接收第一个int型数据,没有就使用默认值0
int score = intent.getIntExtra("score",0);//没有使用bundle时值为0,使用后为100,bundle接受数据
String name = intent.getStringExtra("name");
String[] aihao = intent.getStringArrayExtra("aihao");
//自定义类User.class接收数据
Serializable user = (User)intent.getSerializableExtra("user");
Log.i("MainActivity_sec",age+" 显示年龄");
Log.i("MainActivity_sec",score+" 显示分数");
Log.i("MainActivity_sec",name+" 显示姓名");
Log.i("MainActivity_sec", Arrays.toString(aihao));
Log.i("MainActivity_sec",user.toString());//打印user自定义数据
}
public void back(View v){//“返回”按钮的方法
Intent intent = new Intent();
intent.putExtra("msg","这是从MainActivity返回来的数据");
setResult(200,intent);
finish();//关闭activty,效果等同于返回按钮
}
}
第三步:运行APP
四、回传值与消息内容
package com.xwb.userintent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.renderscript.RenderScript;
import android.util.Log;
import android.view.View;
import com.xwb.userintent.model.User;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
public void tiaozhuan(View v){
Intent intent = new Intent();//跳转意图
//告诉意图,想从哪个界面跳转到哪里
intent.setClass(MainActivity.this,MainActivity_sec.class);//跳转的源、目的
//发送数据,java基本数据类型有8类,不支持Map类型。设置完数据后,需要到MainActivity_sec.activity中接收数据
intent.putExtra("name","haha");
intent.putExtra("age",20);//int型数据
intent.putExtra("aihao",new String[]{"唱歌","打游戏"});
//使用Bundle,发送数据
Bundle bundle = new Bundle();//创建
bundle.putInt("score",100);//设置score值为100
bundle.putString("info","这里是Bundle传值");
intent.putExtras(bundle);//关键处,把bundle值传给intent
//传自定义数据,发送数据
User user = new User("lidada","123456",40);
intent.putExtra("user",user);
//startActivity(intent);//执行跳转
startActivityForResult(intent,100);//第1步设置回传值
}
//第2步重新方法
//requestCode请求码与startActivityForResult()中requestCode一致;
// resultCode结果码,由上一个页面返回的setResult()设置;
// Intent data上一个界面的返回的数据对象
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("MainActivity","onActivityResult");//打印该方法是否回传数据值信息
switch (requestCode){//通过requestCode值来判断执行事件
case 100:
String msg = data.getStringExtra("msg");
Log.i("MainActivity",""+requestCode+"."+resultCode+"."+msg);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
运行APP
-------------------------------------------------------------------------------------------------------------------
完整代码
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_200"
tools:context=".MainActivity">
<Button
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Activity会跳转的界面"
android:onClick="tiaozhuan"
/>
</LinearLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_500"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:onClick="back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"/>
<Button
android:id="@+id/sec"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Activity的显示界面"/>
</LinearLayout>
package com.xwb.userintent;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.renderscript.RenderScript;
import android.util.Log;
import android.view.View;
import com.xwb.userintent.model.User;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
public void tiaozhuan(View v){
Intent intent = new Intent();//跳转意图
//告诉意图,想从哪个界面跳转到哪里
intent.setClass(MainActivity.this,MainActivity_sec.class);//跳转的源、目的
//发送数据,java基本数据类型有8类,不支持Map类型。设置完数据后,需要到MainActivity_sec.activity中接收数据
intent.putExtra("name","haha");
intent.putExtra("age",20);//int型数据
intent.putExtra("aihao",new String[]{"唱歌","打游戏"});
//使用Bundle,发送数据
Bundle bundle = new Bundle();//创建
bundle.putInt("score",100);//设置score值为100
bundle.putString("info","这里是Bundle传值");
intent.putExtras(bundle);//关键处,把bundle值传给intent
//传自定义数据,发送数据
User user = new User("lidada","123456",40);
intent.putExtra("user",user);
//startActivity(intent);//执行跳转
startActivityForResult(intent,100);//第1步设置回传值
}
//第2步重新方法
//requestCode请求码与startActivityForResult()中requestCode一致;
// resultCode结果码,由上一个页面返回的setResult()设置;
// Intent data上一个界面的返回的数据对象
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("MainActivity","onActivityResult");//打印该方法是否回传数据值信息
switch (requestCode){//通过requestCode值来判断执行事件
case 100:
String msg = data.getStringExtra("msg");
Log.i("MainActivity",""+requestCode+"."+resultCode+"."+msg);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
package com.xwb.userintent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import com.xwb.userintent.model.User;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.Arrays;
public class MainActivity_sec extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sec);
//接收MainActivity的传过来的数据值,并保存到相应值中
Intent intent = getIntent();
int age = intent.getIntExtra("age",0);//接收第一个int型数据,没有就使用默认值0
int score = intent.getIntExtra("score",0);//没有使用bundle时值为0,使用后为100,bundle接受数据
String name = intent.getStringExtra("name");
String[] aihao = intent.getStringArrayExtra("aihao");
//自定义类User.class接收数据
Serializable user = (User)intent.getSerializableExtra("user");
Log.i("MainActivity_sec",age+" 显示年龄");
Log.i("MainActivity_sec",score+" 显示分数");
Log.i("MainActivity_sec",name+" 显示姓名");
Log.i("MainActivity_sec", Arrays.toString(aihao));
Log.i("MainActivity_sec",user.toString());//打印user自定义数据
}
public void back(View v){//“返回”按钮的方法
Intent intent = new Intent();
intent.putExtra("msg","这是从MainActivity返回来的数据");
setResult(200,intent);
finish();//关闭activty,效果等同于返回按钮
}
}
package com.xwb.userintent.model;
import java.io.Serializable;
/**JavaBean**/
public class User implements Serializable {//在该自定义User类放置自定义的部分数据,大部分情况数据来源于自定义类,如:数据库也多用于此类
private String name;
private String password;
private int age;
public User(){
}
public User(String name,String password,int age){
this.name = name;
this.password = password;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xwb.userintent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Design.NoActionBar">
<activity android:name=".MainActivity_sec">/
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 28
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.xwb.userintent"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}