传送值核心代码:
intent.putExtra(“cute”,textView.getText().toString());
intent.putExtra(“number”,textView1.getText().toString());
注意:startActivity(intent);这个一定要放在上面两行代码后面,否则传值会失败哦!
接收值核心代码:
Intent intent=getIntent();
String cute=intent.getStringExtra(“cute”);
String number=intent.getStringExtra(“number”);
( String number=getIntent().getStringExtra(“number”);可以这样写)
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="148dp" android:text="跳转second" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="今天不是厌学的一天,今天的好学的一天耶!" android:textSize="15sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="跳转third" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/button" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="244dp" android:text="可爱多一点" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" app:layout_constraintVertical_bias="1.0" /> </androidx.constraintlayout.widget.ConstraintLayout>
package com.example.day_22; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显示启动 Intent intent=new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); //隐式启动 // Intent intent1=new Intent("com.example.day_22.MainActivity2"); // startActivity(intent1); } }); Button button1=findViewById(R.id.button2); TextView textView=findViewById(R.id.textView); TextView textView1=findViewById(R.id.textView4); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MainActivity3.class); //传单个数据 intent.putExtra("cute",textView.getText().toString()); intent.putExtra("number",textView1.getText().toString()); startActivity(intent); } }); } }
package com.example.day_22; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class MainActivity3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); Intent intent=getIntent(); TextView textView=findViewById(R.id.textView2); TextView textView1=findViewById(R.id.textView3); //cute,number都需要和MainActivity里面的一样,就相当于id,是唯一的。 String cute=intent.getStringExtra("cute"); String number=intent.getStringExtra("number"); textView.setText(cute); textView1.setText(number); } }
跳转前
跳转后
当我们需要传送的对象比较多的时候,我们可以写到一个类中,然后传送。
需要继承Serializable接口
Serializable接口就是Java提供用来进行高效率的异地共享实例对象的机制,实现这个接口即可。
什么是Serializable接口呢?
一个对象序列化的接口,一个类只有实现了Serializable接口,它的对象才能被序列化。
什么是序列化?
把对象转换为字节序列的过程称为对象的序列化
把字节序列恢复为对象的过程称为对象的反序列化
为什么要序列化对象?
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
什么情况下需要序列化?
当我们需要把对象的状态信息通过网络进行传输,或者需要将对象的状态信息持久化,以便将来使用时都需要把对象进行序列化
import java.io.Serializable; public class Kind implements Serializable { String name; String age; String weight; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getWeight() { return weight; } public void setWeight(String weight) { this.weight = weight; } }
核心代码:
Kind kind=new Kind();
kind.setName(textView.getText().toString());
kind.setWeight(textView1.getText().toString());
intent.putExtra(“Kind”,kind);
核心代码:
Kind kind=(Kind)getIntent().getSerializableExtra(“Kind”);
textView.setText(kind.getName());
textView1.setText(kind.getWeight());
package com.example.day_22; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView age; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //显示启动 Intent intent=new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); //隐式启动 // Intent intent1=new Intent("com.example.day_22.MainActivity2"); // startActivity(intent1); } }); Button button1=findViewById(R.id.button2); TextView textView=findViewById(R.id.textView); TextView textView1=findViewById(R.id.textView4); age=findViewById(R.id.textView5); Kind kind=new Kind(); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MainActivity3.class); //传单个数据 // intent.putExtra("cute",textView.getText().toString()); // intent.putExtra("number",textView1.getText().toString()); kind.setName(textView.getText().toString()); kind.setWeight(textView1.getText().toString()); intent.putExtra("Kind",kind); startActivityForResult(intent,0); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); String str=data.getStringExtra("age"); age.setText(str); } }
其实意思是一样的,就是在跳转之后的里面在写传值,然后跳转到之前的Activity。
package com.example.day_22; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); TextView textView=findViewById(R.id.textView2); TextView textView1=findViewById(R.id.textView3); EditText editText=findViewById(R.id.edit); Kind kind=(Kind)getIntent().getSerializableExtra("Kind"); textView.setText(kind.getName()); textView1.setText(kind.getWeight()); Button button=findViewById(R.id.button3); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=getIntent(); intent.putExtra("age",editText.getText().toString()); setResult(1,intent); //回到上一个Activity finish(); } }); } }