第9天~接续第2页-

接续第2页-
可参考什麽是Intent意图? https://litotom.com/ch5-2-intent/
//打包资料
建立一个包裹然後塞东西
bundle.putString("name",name);就是map的写法:key和value

https://ithelp.ithome.com.tw/upload/images/20220131/20119035v5W3s69y96.png

//建立Intent物件
        Intent it = new Intent();//送货员
        it.setClass(MainActivity.this,Page2.class);//送到哪里去

        //打包资料
        Bundle bundle = new Bundle();
        bundle.putString("name",name);
        bundle.putString("phone",phone);
        bundle.putInt("num",num);
        bundle.putString("meal",mealString);
        bundle.putString("drink",drinkString);
        bundle.putString("memo",all);
        //Intent夹带资料包裹
        it.putExtras(bundle);

        //送到第2页
        startActivity(it);//出发了

目前第2页还空白的

https://ithelp.ithome.com.tw/upload/images/20220131/20119035cLcg69raI2.png


第2页还空白的程序码;

删掉androidx.constraintlayout.widget.ConstraintLayout改成LinearLayout

https://ithelp.ithome.com.tw/upload/images/20220131/20119035ylpQrevbQ0.png

https://ithelp.ithome.com.tw/upload/images/20220131/20119035RCfTowrR3D.png

LinearLayout-丢进来就可以依序排列.看到xml档

https://ithelp.ithome.com.tw/upload/images/20220131/20119035IZWBB81J0n.png


开始放入6个文字栏位textView+加入2个按钮

https://ithelp.ithome.com.tw/upload/images/20220131/20119035gmiMmMHxGW.png

要美化..加入内距不可以放东西-padding-30dp

https://ithelp.ithome.com.tw/upload/images/20220131/20119035SmC4R3pu6U.png


改字的大小20sp一起选可以一起改
https://ithelp.ithome.com.tw/upload/images/20220131/2011903503V2K4L4HH.png


用layout_margin Top分开
https://ithelp.ithome.com.tw/upload/images/20220131/20119035dijVOZmMzQ.png

button也要分开-
https://ithelp.ithome.com.tw/upload/images/20220131/20119035uuciDWYI6O.png


button要编辑文字+onClick

https://ithelp.ithome.com.tw/upload/images/20220131/20119035Q0qupa5xVK.png

回java程序码onClick-->用这个不用加id

https://ithelp.ithome.com.tw/upload/images/20220131/20119035zJVUW9b0kH.png

https://ithelp.ithome.com.tw/upload/images/20220131/201190350Pg23wKiAh.png

第2页的初始化-

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);
        

    }

    public void onClick(View view) {
    }
}

接收包裹-迎接第一页送过来的+用map的方式key和value

从put变get

key在程序码不会显示,所以用截图

https://ithelp.ithome.com.tw/upload/images/20220131/20119035lqwnAAkm3v.png

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");
            }

    public void onClick(View view) {
    }
}

继续编辑显示:

https://ithelp.ithome.com.tw/upload/images/20220131/20119035yXOZrpPmyB.png

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");

        t1.setText("订购人"+name);
        t2.setText(phone);
        t3.setText(num);
        t4.setText(meal);
        t5.setText(drink);
        t6.setText(memo);

        //小计
        


            }

    public void onClick(View view) {
    }
}

再来是小计:文字要去比对内容要用equals不要用=
用toast显示就要用手机试

https://ithelp.ithome.com.tw/upload/images/20220131/20119035AxJSleCf5B.jpg

https://ithelp.ithome.com.tw/upload/images/20220131/201190352ywbhVZ2Ew.png

//小计
        int sum =0;
        if(meal.equals("鸡肉")){
            sum =num * 80;
        }else if(meal.equals("猪肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鲜")){
            sum =num*70;
        }

再写入toast显示:

//小计
        int sum =0;
        if(meal.equals("鸡肉")){
            sum =num * 80;
        }else if(meal.equals("猪肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鲜")){
            sum =num*70;
        }
        Toast.makeText(Page2.this,"小计:"+sum+"元",Toast.LENGTH_LONG).show();

目前完整的程序码:

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");

        t1.setText("订购人"+name);
        t2.setText(phone);
        t3.setText(num);
        t4.setText(meal);
        t5.setText(drink);
        t6.setText(memo);

        //小计
        int sum =0;
        if(meal.equals("鸡肉")){
            sum =num * 80;
        }else if(meal.equals("猪肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鲜")){
            sum =num*70;
        }
        Toast.makeText(Page2.this,"小计:"+sum+"元",Toast.LENGTH_LONG).show();
    }

    public void onClick(View view) {
    }
}

再加上t7因为Toast会消失
https://ithelp.ithome.com.tw/upload/images/20220131/201190358QaK7ut2nt.png

java程序档也要改

加入t7

https://ithelp.ithome.com.tw/upload/images/20220201/20119035xIL22hK7ou.png

原来的Toast

Toast.makeText(Page2.this,"小计:"+sum+"元",Toast.LENGTH_LONG).show();

改成-

t7.setText("小计:"+sum+"元");

跑一下

https://ithelp.ithome.com.tw/upload/images/20220201/20119035eCv4cLlS6T.png

目前是第2页程序码:

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6,t7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);
        t7 =findViewById(R.id.t7);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");

        t1.setText("订购人"+name);
        t2.setText(phone);
        t3.setText(num);
        t4.setText(meal);
        t5.setText(drink);
        t6.setText(memo);


        //小计
        int sum =0;
        if(meal.equals("鸡肉")){
            sum =num * 80;
        }else if(meal.equals("猪肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鲜")){
            sum =num*70;
        }

        t7.setText("小计:"+sum+"元");

        //Toast.makeText(Page2.this,"小计:"+sum+"元",Toast.LENGTH_LONG).show();
    }

    public void onClick(View view) {
    }
}

目前是第2页模拟器跳不过去-後面更新应该会在第11天~




<<:  Day 10. 状况篇 Zabbix 安装问题排除

>>:  D14 - 用 Swift 和公开资讯,打造投资理财的 Apps { 加权指数K线图实作.2 }

Epoch 31 - 再启程

Hi, 大家好, 这是第二次参加铁人赛,继续前一年的纪录吧!(懒到连标题都延用) 去年的传送门: h...

使用证书对代码进行签章,以防止其被篡改并向用户验证您的身份-使用您的私钥对代码进行散列并加密结果

-使用私钥和公钥对强大的程序集进行签名和验证 (来源:https://flylib.com/boo...

day12: 模组化好的写法-为什麽要模组化

在过去 Javascript还没发展 common.js 或是 ESM,在使用 Javascript...

Day20 React 使用Bootstrap 5快速建立网页

首先先要安装Bootstrap 5的套件 安装Bootstrap 5套件连结 •Install wi...

Day_01: 让 Vite 来开启你的Vue 前言

Hi Da Gei Ho~ 初次见面,我是Winnie~ 我是一位刚转职六个月的菜鸟前端(前身是网页...