數據傳遞與數據回傳


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="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="horizontal">
 6     <TextView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:text="水果:"/>
10 
11     <LinearLayout
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         android:orientation="vertical">
15         <CheckBox
16             android:id="@+id/apple"
17             android:layout_width="wrap_content"
18             android:layout_height="wrap_content"
19             android:text="蘋果"/>
20         <CheckBox
21             android:id="@+id/orange"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:text="橙子"/>
25         <CheckBox
26             android:id="@+id/watermelon"
27             android:layout_width="wrap_content"
28             android:layout_height="wrap_content"
29             android:text="西瓜"/>
30         <CheckBox
31             android:id="@+id/grape"
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content"
34             android:text="葡萄"/>
35 
36         <Button
37             android: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="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <LinearLayout
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:orientation="vertical">
11 
12         <TextView
13             android:layout_width="wrap_content"
14             android:layout_height="wrap_content"
15             android:text="選擇了:" />
16 
17         <TextView
18             android:id="@+id/fruit"
19             android:layout_width="wrap_content"
20             android:layout_height="wrap_content" />
21 
22         <TextView
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:text="商家是:" />
26 
27         <TextView
28             android:id="@+id/shop"
29             android:layout_width="wrap_content"
30             android:layout_height="wrap_content" />
31 
32     </LinearLayout>>
33 
34     <Button
35         android: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="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:orientation="horizontal"
 5     android:layout_width="wrap_content"
 6     android:layout_height="wrap_content">
 7     <ListView
 8         android: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()

1 public 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 = intent.getStringExtra("extra_data");

那就好說了,寫代碼嘍

 1 Button tijiao = (Button) findViewById(R.id.tijiao);
 2        tijiao.setOnClickListener(new View.OnClickListener() {
 3             @Override
 4             public void onClick(View view) {
 5                 String str="";//保存所有選中的值
 6                 if(apple.isChecked())//選中蘋果
 7                     str+=apple.getText().toString()+"";
 8                 if(orange.isChecked())//選中橘子
 9                     str+=orange.getText().toString()+"";
10                 if(watermelon.isChecked())//選中西瓜
11                     str+=watermelon.getText().toString()+"";
12                 if(grape.isChecked())//選中葡萄
13                     str+=grape.getText().toString();
14                 buy_fruits = str;
15                 if(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 = buy_fruits.getStringExtra("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回傳數據
1 protected void onActivityResult(int requestcode,int resultcode,Intent data){
2         super.onActivityResult(requestcode,resultcode,c);
3         if (requestcode == 1 && resultcode == 1){
4             String string = data.getStringExtra("extra_data");
5         }
6     }
//重寫onActivityResult()方法用來接收數據

然后我們就可以根據上面的代碼來寫我們的代碼了

 1 public void c_shops() {
 2         Intent intent = new Intent(this,thirdActivity.class);
 3         startActivityForResult(intent,1);
 4     }
 5 protected void onActivityResult(int requestcode,int resultcode,Intent c){
 6         super.onActivityResult(requestcode,resultcode,c);
 7         if (requestcode == 1 && resultcode == 1){
 8             String shops = c.getStringExtra("shop");
 9             shop = (TextView) findViewById(R.id.shop);
10             shop.setText(shops);
11         }
12     }
1 public 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的全部代碼

package com.example.app;

import androidx.appcompat.app.AppCompatActivity;
import android.support.v4.app.*;
import androidx.appcompat.app.AlertDialog;
import android.text.TextUtils;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.content.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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homework_seven_1);

        apple = (CheckBox) findViewById(R.id.apple);
        orange = (CheckBox) findViewById(R.id.orange);
        watermelon = (CheckBox) findViewById(R.id.watermelon);
        grape = (CheckBox) findViewById(R.id.grape);

        Button tijiao = (Button) findViewById(R.id.tijiao);
        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String str="";//保存所有選中的值
                if(apple.isChecked())//選中蘋果
                    str+=apple.getText().toString()+"";
                if(orange.isChecked())//選中橘子
                    str+=orange.getText().toString()+"";
                if(watermelon.isChecked())//選中西瓜
                    str+=watermelon.getText().toString()+"";
                if(grape.isChecked())//選中葡萄
                    str+=grape.getText().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);

    }
}
package com.example.app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import android.util.Log;
import android.text.TextUtils;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.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) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.homework_seven_2);
        Intent buy_fruits = getIntent();
        String fruits = buy_fruits.getStringExtra("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){
        super.onActivityResult(requestcode,resultcode,c);
        if (requestcode == 1 && resultcode == 1){
            String shops = c.getStringExtra("shop");
            shop = (TextView) findViewById(R.id.shop);
            shop.setText(shops);
        }
    }
}
package com.example.app;

import android.os.*;
import android.view.View;
import android.widget.*;
import android.content.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) {
        super.onCreate(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();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM