改標題:
先起個頭~繼續Intent~官網資料
https://developer.android.com/reference/android/content/Intent
第9天卡住的第2頁的跳轉
main ->intent->page2
發現是
t3.setText("數量"+num);
要從int變String不然會閃退
修改的page2.java
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頁有做了~所以第一頁的小計拿掉
//TextView uPrice;
//uPrice =findViewById(R.id.uPrice);
再來寫-取消訂單button-用page2再送一個intent回來
再到xml檔onClick2
在MainActivity.java檔第一頁的"取消訂單"button
public void onClick2(View view) {
uName.setText("");
uPhone.setText("");
uNum.setText("");
}
目前全部完成的程式碼:
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
//TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uName =findViewById(R.id.uName);
uPhone =findViewById(R.id.uPhone);
uNum =findViewById(R.id.uNum);
//uPrice =findViewById(R.id.uPrice);
//uMeal
uMeal=findViewById(R.id.uMeal);
uMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
mealString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//uDrink
uDrink=findViewById(R.id.uDrink);
uDrink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
drinkString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//checkBox
memo1 =findViewById(R.id.memo1);
memo2 =findViewById(R.id.memo2);
memo3 =findViewById(R.id.memo3);
}
//收集資料並送到下一頁
public void onClick(View view) {
//文字欄位的收集
String name = uName.getText().toString();
String phone = uPhone.getText().toString();
int num = Integer.parseInt(uNum.getText().toString());
//checkBox
int[] cbs ={R.id.memo1,R.id.memo2,R.id.memo3};
for(int i:cbs){
CheckBox cc =findViewById(i);
if(cc.isChecked()){
all += cc.getText().toString()+"\t";
}
}
//建立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);//出發了
}
public void onClick2(View view) {
uName.setText("");
uPhone.setText("");
uNum.setText("");
}
}
姓名 必填-不可以空的語法-
String name = uName.getText().toString();
用name 不是uName
TextUtils是檢查文字的工具-
https://codertw.com/android-%E9%96%8B%E7%99%BC/335221/
//文字欄位的收集
String name = uName.getText().toString();
if(!TextUtils.isEmpty(name)){
Toast.makeText(MainActivity.this,"不可為空",Toast.LENGTH_SHORT).show();
}
目前全部的程式碼:
package com.huang.myapplication8;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//宣告變數
EditText uName,uPhone,uNum;
//TextView uPrice;
RadioGroup uMeal,uDrink;
RadioButton m1,m2,m3,m4,d1,d2,d3;
CheckBox memo1,memo2,memo3;
String mealString,drinkString,all ="";
//初始化
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uName =findViewById(R.id.uName);
uPhone =findViewById(R.id.uPhone);
uNum =findViewById(R.id.uNum);
//uPrice =findViewById(R.id.uPrice);
//uMeal
uMeal=findViewById(R.id.uMeal);
uMeal.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
mealString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//uDrink
uDrink=findViewById(R.id.uDrink);
uDrink.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb =findViewById(checkedId);
drinkString = rb.getText().toString();
Toast.makeText(MainActivity.this,mealString,Toast.LENGTH_SHORT).show();
}
});
//checkBox
memo1 =findViewById(R.id.memo1);
memo2 =findViewById(R.id.memo2);
memo3 =findViewById(R.id.memo3);
}
//收集資料並送到下一頁
public void onClick(View view) {
//文字欄位的收集
String name = uName.getText().toString();
if(!TextUtils.isEmpty(name)){
Toast.makeText(MainActivity.this,"不可為空",Toast.LENGTH_SHORT).show();
}
String phone = uPhone.getText().toString();
int num = Integer.parseInt(uNum.getText().toString());
//checkBox
int[] cbs ={R.id.memo1,R.id.memo2,R.id.memo3};
for(int i:cbs){
CheckBox cc =findViewById(i);
if(cc.isChecked()){
all += cc.getText().toString()+"\t";
}
}
//建立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);//出發了
}
public void onClick2(View view) {
uName.setText("");
uPhone.setText("");
uNum.setText("");
}
}