MainActivity类进行传值
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void intentbase(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("name","wxl");
intent.putExtra("age",20);
intent.putExtra("money",270.2f);
startActivity(intent);
}
public void intentArray(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
String[] names = new String[]{"oldwang", "laownag", "wxl"};
int[] ages = new int[]{50,18,20};
intent.putExtra("ages",ages);
intent.putExtra("names",names);
startActivity(intent);
}
public void intentArraylsit(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
ArrayList<String> stringArrayList=new ArrayList<>();
stringArrayList.add("laowang");
stringArrayList.add("oldwang");
stringArrayList.add("wxl");
intent.putStringArrayListExtra("namesArrayList",stringArrayList);
ArrayList<Integer> integerArrayList=new ArrayList<>();
integerArrayList.add(50);
integerArrayList.add(18);
integerArrayList.add(20);
intent.putIntegerArrayListExtra("integerArrayList",integerArrayList);
startActivity(intent);
}
public void intentlsit(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
List<String> stringList=new ArrayList<>();
stringList.add("oldwang");
stringList.add("laowang");
stringList.add("wxl");
intent.putExtra("stringList",(Serializable) stringList);
List<Integer> integerList=new ArrayList<>();
integerList.add(20);
integerList.add(40);
integerList.add(18);
intent.putExtra("integerList",(ArrayList)integerList);
List<Map<String,Object>> mapList=new ArrayList<>();
Map<String,Object> stringObjectMap=new HashMap<>();
stringObjectMap.put("name","laowang");
stringObjectMap.put("age",18);
stringObjectMap.put("money",150.2f);
mapList.add(stringObjectMap);
intent.putExtra("mapList",(Serializable) mapList);
startActivity(intent);
}
public void intentbundle(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
Bundle bundle=new Bundle();
bundle.putString("name","wxl");
bundle.putInt("age",18);
bundle.putFloat("money",200.2f);
String [] names=new String[]{"oldwang","laowang","wxl"};
bundle.putStringArray("names",names);
ArrayList<String> stringArrayList=new ArrayList<>();
stringArrayList.add("olwang");
stringArrayList.add("laowang");
stringArrayList.add("wxl");
bundle.putStringArrayList("stringArrayList",stringArrayList);
List<Integer> integerList=new ArrayList<>();
integerList.add(18);
integerList.add(20);
integerList.add(50);
bundle.putSerializable("lists",(Serializable) integerList);
intent.putExtras(bundle);
startActivity(intent);
}
public void intentObject(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
User user=new User("wxl",20,200.2f);
intent.putExtra("user",user);
User user1=new User("xx11",18,150.2f);
User user2=new User("xx22",20,200.2f);
List<User> userList=new ArrayList<>();
userList.add(user1);
userList.add(user2);
intent.putExtra("userList",(Serializable) userList);
startActivity(intent);
}
public void intentApp(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
MyApp myApp=(MyApp) getApplication();
Map<String,Object> stringObjectMap=new HashMap<>();
stringObjectMap.put("name","wxl");
stringObjectMap.put("age",18);
stringObjectMap.put("money",200.2f);
myApp.mapList.add(stringObjectMap);
startActivity(intent);
}
public void intentstatic(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
Map<String,Object> stringObjectMap=new HashMap<>();
stringObjectMap.put("name","laownag");
stringObjectMap.put("age",50);
stringObjectMap.put("money",230.2f);
MyStatic.mapList.add(stringObjectMap);
startActivity(intent);
}
public void intentforResult(View view) {
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("msg","来办公室");
startActivityForResult(intent,200);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==200&&resultCode==110)
{
String msg2=data.getStringExtra("msg2");
Toast.makeText(MainActivity.this,msg2+"你们自己处理"+"再来1000个",Toast.LENGTH_LONG).show();
}
}
}
Main2Activity类进行接收
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void intentbase(View view) {
Intent intent=getIntent();
String name=intent.getStringExtra("name");
int age=intent.getIntExtra("age",18);
float money=intent.getFloatExtra("money",150.3f);
Log.i("---name", name+age+money);
}
public void intentArray(View view) {
Intent intent=getIntent();
String[] names=intent.getStringArrayExtra("names");
int[] ages=intent.getIntArrayExtra("ages");
Log.i("====names", names[0]+ages[1]);
}
public void intentArraylsit(View view) {
Intent intent=getIntent();
ArrayList<String> stringArrayList=intent.getStringArrayListExtra("namesArrayList");
ArrayList<Integer> integerArrayList=intent.getIntegerArrayListExtra("integerArrayList");
Log.i("----stringArrayList", stringArrayList.get(0)+integerArrayList.get(1));
}
public void intentlsit(View view) {
Intent intent=getIntent();
List<String> stringList=(List<String>) intent.getSerializableExtra("stringList");
List<Integer> integerList=intent.getIntegerArrayListExtra("integerList");
List<Map<String,Object>> mapList=(List<Map<String, Object>>) intent.getSerializableExtra("mapList");
Log.i("----stringList", stringList.get(0)+integerList.get(2)+mapList.get(0).get("name"));
}
public void intentbundle(View view) {
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
String name=bundle.getString("name");
String[] names=bundle.getStringArray("names");
ArrayList<String> arrayList=bundle.getStringArrayList("stringArrayList");
List<Integer> integerList=(List<Integer>) bundle.getSerializable("lists");
Log.i("----name", name+names[0]+arrayList.get(1)+integerList.get(2));
}
public void intentObject(View view) {
Intent intent=getIntent();
User user=(User) intent.getSerializableExtra("user");
List<User> userList=(List<User>) intent.getSerializableExtra("userList");
Log.i("----user", user.name+userList.get(1).name);
}
public void intentApp(View view) {
Intent intent=getIntent();
MyApp myApp=(MyApp) getApplication();
Map<String,Object> stringObjectMap=myApp.mapList.get(0);
String name=stringObjectMap.get("name").toString();
Log.i("----namezxzx", name);
}
public void intentstatic(View view) {
Intent intent=getIntent();
Map<String,Object> stringObjectMap=MyStatic.mapList.get(0);
String nammmm=stringObjectMap.get("name").toString();
Log.i("----nammmm", nammmm);
}
public void intentforResult(View view) {
Intent intent=getIntent();
String msg=intent.getStringExtra("msg");
intent.putExtra("msg2","我告诉警察叔叔");
Log.i("----msg", msg);
setResult(110,intent);
finish();
}
}
MyApp类
import android.app.Application;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MyApp extends Application {
public List<Map<String,Object>> mapList=new ArrayList<>();
}
MyStatic类
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MyStatic {
public static List<Map<String,Object>> mapList=new ArrayList<>();
}
User类
import java.io.Serializable;
public class User implements Serializable {
String name;
int age;
float money;
public User(String name, int age, float money) {
this.name = name;
this.age = age;
this.money = money;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
}
activity_main.xml是MainActivity的UI布局
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:text="第一界面"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:text="传递基本数据类型"
android:onClick="intentbase"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递数组"
android:onClick="intentArray"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递Arraylsit"
android:onClick="intentArraylsit"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递lsit"
android:onClick="intentlsit"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递bundle"
android:onClick="intentbundle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递Object"
android:onClick="intentObject"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递App"
android:onClick="intentApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递static"
android:onClick="intentstatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="传递forResult"
android:onClick="intentforResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
activity_main2.xml是MainActivity2的布局
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<TextView
android:text="第二个界面"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<Button
android:text="接收基本数据类型"
android:onClick="intentbase"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收数组"
android:onClick="intentArray"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收Arraylsit"
android:onClick="intentArraylsit"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收lsit"
android:onClick="intentlsit"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收bundle"
android:onClick="intentbundle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收Object"
android:onClick="intentObject"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收App"
android:onClick="intentApp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收static"
android:onClick="intentstatic"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="接收forResult"
android:onClick="intentforResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>