数据传递与数据回传
1.按照图(1)布局编写第⼀个Activity,点击按钮后跳转到第⼆个Activity并将选择的结果数据⼀起传递过去。
2.按照图(2)编写第⼆个Activity,显⽰第⼀个Activity传递的数据内容,点击按钮后跳转到第三个Activity。
3.按照图(3)编写第三个Activity,⽤ListView实现列表展⽰信息,点击列表某⾏信息后跳转回第⼆个Activity,并将列表中数据回传给第⼆个Activity,并且在第⼆个Activity中对信息进⾏展⽰。
根据上图,先写出xml布局:
第⼀个
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="schemas.android/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent"
5    android:orientation="horizontal">
6<TextView
7android:layout_width="wrap_content"
8        android:layout_height="wrap_content"
9        android:text="⽔果:"/>
10
11<LinearLayout
12android:layout_width="wrap_content"
13        android:layout_height="wrap_content"
14        android:orientation="vertical">
15<CheckBox
16android:id="@+id/apple"
17            android:layout_width="wrap_content"
18            android:layout_height="wrap_content"
19            android:text="苹果"/>
20<CheckBox
21android:id="@+id/orange"
22            android:layout_width="wrap_content"
23            android:layout_height="wrap_content"
24            android:text="橙⼦"/>
25<CheckBox
26android:id="@+id/watermelon"
27            android:layout_width="wrap_content"
28            android:layout_height="wrap_content"
29            android:text="西⽠"/>
30<CheckBox
31android:id="@+id/grape"
32            android:layout_width="wrap_content"
33            android:layout_height="wrap_content"
34            android:text="葡萄"/>
35
36<Button
37android:id="@+id/tijiao"
38            android:layout_width="wrap_content"
39            android:layout_height="wrap_content"
40            android:text="提交"/>
41</LinearLayout>
42
43</LinearLayout>
第⼆个
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="schemas.android/apk/res/android"
3    android:layout_width="match_parent"
4    android:layout_height="match_parent"
5    android:orientation="vertical">
6
7<LinearLayout
8android:layout_width="wrap_content"
9        android:layout_height="wrap_content"
10        android:orientation="vertical">
11
12<TextView
13android:layout_width="wrap_content"
14            android:layout_height="wrap_content"
15            android:text="选择了:"/>
16
17<TextView
18android:id="@+id/fruit"
19            android:layout_width="wrap_content"
20            android:layout_height="wrap_content"/>
21
22<TextView
23android:layout_width="wrap_content"
24            android:layout_height="wrap_content"
25            android:text="商家是:"/>
26
27<TextView
28android:id="@+id/shop"
29            android:layout_width="wrap_content"
30            android:layout_height="wrap_content"/>
31
32</LinearLayout>>
33
34<Button
35android:id="@+id/c_shop"
36        android:layout_width="wrap_content"
37        android:layout_height="wrap_content"
38        android:text="选择商家"/>
39
40</LinearLayout>
第三个
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="schemas.android/apk/res/android"
3    xmlns:app="schemas.android/apk/res-auto"
4    android:orientation="horizontal"
5    android:layout_width="wrap_content"
6    android:layout_height="wrap_content">
7<ListView
8android:id="@+id/shows"
9        android:layout_width="wrap_content"
10        android:layout_height="wrap_content"/>
11
12</LinearLayout>
需要三个activity,从MainActivity开始,对应图(1)
MainActivity重点是我们要发送页⾯的值给SecondActivity(图2)
我们单独写⼀个⽅法buy_fruit()
1public void buy_fruit() {
2        Intent intent_1 = new Intent(this,SecondActivity.class);
3        intent_1.putExtra("buy_fruits",buy_fruits);
4        startActivity(intent_1);
5
6    }
还有我们⽤多选按钮来进⾏选择的过程,思路就是点击按钮,将选择的⽔果打包起来传出去传递数据需要⽤到Intent提供的putExtra()⽅法
1 Intent intent = new Intent(this,Activity02.class);
2 intent.putExtra("extra_data","Hello Activity02");
3 startActivity(intent);
1 Intent intent = getIntent();
2 String data = StringExtra("extra_data");
那就好说了,写代码喽
1 Button tijiao = (Button) findViewById(R.id.tijiao);
2        tijiao.setOnClickListener(new View.OnClickListener() {
3            @Override
4public void onClick(View view) {
5                String str="";//保存所有选中的值
6if(apple.isChecked())//选中苹果
7                    str+=Text().toString()+"";
8if(orange.isChecked())//选中橘⼦
9                    str+=Text().toString()+"";
10if(watermelon.isChecked())//选中西⽠
11                    str+=Text().toString()+"";
12if(grape.isChecked())//选中葡萄
13                    str+=Text().toString();
14                buy_fruits = str;
15if(buy_fruits.equals("")){
16                    Toast.makeText(MainActivity.this,"不可以空选",Toast.LENGTH_SHORT).show();
17                }else {
18                    Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
19                    buy_fruit();
20                }
21            }
22        });
好了,我们第⼀个activity写完了,开始写SecondActivity
这个是最难写的,它要接收两个值,第⼀个的和第三个的数据回传的值,所以我们可以分成两部分来写,先写接收MainActivity的值,并显⽰在图(2)上
1 Intent buy_fruits = getIntent();
2 String fruits = StringExtra("buy_fruits");
3 fruit = (TextView) findViewById(R.id.fruit);
4 fruit.setText(fruits);
然后写数据回传,这个过程是点击按钮跳转到thirdActivity上,thirdActivity将数据传回来
这个过程的代码是这样的
1 Intent intent = new Intent(this,Active02.class);
2 startActivityForResult(intent,1); //这⾥的1就是rquestCode
//进⾏跳转,数据回传的开始
1 Intent intent = new Intent();
2 intent.putExtra("extra_data","Hello Activity01");
3 setResult(1,intent); //这⾥的1是resultCode
//另⼀个activity回传数据
1protected void onActivityResult(int requestcode,int resultcode,Intent data){
3if (requestcode == 1 && resultcode == 1){
4            String string = StringExtra("extra_data");
5        }
6    }
//重写onActivityResult()⽅法⽤来接收数据
然后我们就可以根据上⾯的代码来写我们的代码了
1public void c_shops() {
2        Intent intent = new Intent(this,thirdActivity.class);
3        startActivityForResult(intent,1);
4    }
5protected void onActivityResult(int requestcode,int resultcode,Intent c){
7if (requestcode == 1 && resultcode == 1){
8            String shops = c.getStringExtra("shop");
9            shop = (TextView) findViewById(R.id.shop);
10            shop.setText(shops);
11        }
12    }
1public void c_shop(String oshop){
2        Intent intent = new Intent();
3        intent.putExtra("shop",oshop);
4        setResult(1,intent);
5        finish();
6    }
secondActivity主要内容也写好了
thirdActiyity主要内容也写好了
附上三个actiyity的全部代码
ample.app;
import androidx.appcompat.app.AppCompatActivity;
import android.support.v4.app.*;
import androidx.appcompat.app.AlertDialog;
TextUtils;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
t.Intent;
public class MainActivity extends AppCompatActivity {
private CheckBox apple;
private CheckBox orange;
private CheckBox watermelon;
private CheckBox grape;
private Button tijiao;
String buy_fruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.homework_seven_1);
apple = (CheckBox) findViewById(R.id.apple);
orange = (CheckBox) findViewById(ange);
watermelon = (CheckBox) findViewById(R.id.watermelon);
grape = (CheckBox) findViewById(ape);
Button tijiao = (Button) findViewById(R.id.tijiao);
tijiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str="";//保存所有选中的值
if(apple.isChecked())//选中苹果
str+=Text().toString()+"";
if(orange.isChecked())//选中橘⼦
str+=Text().toString()+"";
if(watermelon.isChecked())//选中西⽠
str+=Text().toString()+"";
if(grape.isChecked())//选中葡萄
str+=Text().toString();
buy_fruits = str;
if(buy_fruits.equals("")){
Toast.makeText(MainActivity.this,"不可以空选",Toast.LENGTH_SHORT).show();                }else {
Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
buy_fruit();
}
}
});
}
public void buy_fruit() {
Intent intent_1 = new Intent(this,SecondActivity.class);
intent_1.putExtra("buy_fruits",buy_fruits);
startActivity(intent_1);
}
}
ample.app;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import android.util.Log;
TextUtils;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
t.Intent;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private TextView fruit;
private Button c_shop;
private TextView shop;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.homework_seven_2);
Intent buy_fruits = getIntent();
String fruits = StringExtra("buy_fruits");
fruit = (TextView) findViewById(R.id.fruit);
fruit.setText(fruits);
shop = (TextView) findViewById(R.id.shop);
c_shop = (Button) findViewById(R.id.c_shop);
c_shop.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
c_shops();
}
});
}
public void c_shops() {
Intent intent = new Intent(this,thirdActivity.class);
startActivityForResult(intent,1);
}
protected void onActivityResult(int requestcode,int resultcode,Intent c){
if (requestcode == 1 && resultcode == 1){
String shops = c.getStringExtra("shop");
shop = (TextView) findViewById(R.id.shop);
记住我shop.setText(shops);
}
}
}
ample.app;
import android.os.*;
import android.view.View;
import android.widget.*;
t.Intent;
import androidx.appcompat.app.AppCompatActivity;
public class thirdActivity extends AppCompatActivity {
private static String[] shops = new String[]{
"⽢福园旗舰店","福瑞达⽔果汇","沁园春百果园","西域美⾷品"
};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.homework_seven_3);
lv = (ListView) findViewById(R.id.shows);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,shops);//新建并配置ArrayAapeter        lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ //设置监听
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (i) {
case 0:
c_shop("⽢福园旗舰店");
break;
case 1:
c_shop("福瑞达⽔果汇");
break;
case 2:
c_shop("沁园春百果园");
break;
case 3:
c_shop("西域美⾷品");
break;
}
}
});
}
public void c_shop(String oshop){
Intent intent = new Intent();
intent.putExtra("shop",oshop);
setResult(1,intent);
finish();
}
}