zoukankan      html  css  js  c++  java
  • 数据传递与数据回传

    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();
        }
    }
  • 相关阅读:
    文件上传的核心点 一(59)
    ajax1—php(27)
    php—Smarty-缓存2(26)
    php—Smarty-缓存1(25)
    java—实现一个监听器HttpServletRequest的创建销毁、在线人数 (56)
    Linux—virtualbox系统安装(1)
    php—Smarty-2
    java—不同的用户登录以后可以看到不同的菜单(后台可以实现对用户菜单的管理) 1 (55)
    java—数据存储过程 (54)
    java—ThreadLocal模式与OSIV模式(53)
  • 原文地址:https://www.cnblogs.com/rebirther/p/12877786.html
Copyright © 2011-2022 走看看