接續第2頁-
可參考什麼是Intent意圖? https://litotom.com/ch5-2-intent/
//打包資料
建立一個包裹然後塞東西
bundle.putString("name",name);就是map的寫法:key和value
//建立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頁還空白的
第2頁還空白的程式碼;
刪掉androidx.constraintlayout.widget.ConstraintLayout改成LinearLayout
LinearLayout-丟進來就可以依序排列.看到xml檔
開始放入6個文字欄位textView+加入2個按鈕
要美化..加入內距不可以放東西-padding-30dp
改字的大小20sp一起選可以一起改
用layout_margin Top分開
button也要分開-
button要編輯文字+onClick
回java程式碼onClick-->用這個不用加id
第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在程式碼不會顯示,所以用截圖
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) {
}
}
繼續編輯顯示:
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顯示就要用手機試
//小計
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會消失
java程式檔也要改
加入t7
原來的Toast
Toast.makeText(Page2.this,"小計:"+sum+"元",Toast.LENGTH_LONG).show();
改成-
t7.setText("小計:"+sum+"元");
跑一下
目前是第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天~