繁体   English   中英

在Android中通过两个不同的活动添加两个Double值

[英]Adding two Double values in android from two different activities

我正在做一个项目,我陷入了困境,我有3个活动:1-ItemMenu,2- ColdDrinks,3- HotDrinks

ItemMenu活动中有一个按钮,用于计算从ColdDrinks和HotDrinks购买的物品的总价值,当我按下该按钮时,它应该从ColdDrinks和HotDrinks Activity中获得额外的东西,并将两个屏幕的总和相加并显示总计ItemMenu Activity中的textview,问题是它显示ColdDrinks活动的总数并且可以正常工作,但是不能正确显示HotDrinks活动的总数,当我添加新按钮从HotDrinks活动中获得额外收益时,它可以工作,但是我不想为每个活动都拥有一个按钮,我只想一个按钮来添加ColdDrinks活动的总和和HotDrinks活动的总和,而只需在textview中给我总计

这是我的3个活动的代码:

1-ItemMenu活动:

public class ItemMenu extends AppCompatActivity {

Button cDrinks;
Button hDrinks;
Button sandwiches;
Button snacks;
Button meat;
Button chicken;
Button water;
Button snooker;
Button billiards;
Button qallayat;
Button hookah;
TextView total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_item_menu);

    cDrinks      =       (  Button  )   findViewById        (  R.id.       cDrinks  )         ;
    hDrinks      =       (  Button  )   findViewById        (  R.id.       hDrinks  )         ;
    sandwiches   =       (  Button  )   findViewById        (  R.id.    sandwiches  )         ;
    snacks       =       (  Button  )   findViewById        (  R.id.        snacks  )         ;
    meat         =       (  Button  )   findViewById        (  R.id.          meat  )         ;
    chicken      =       (  Button  )   findViewById        (  R.id.       chicken  )         ;
    water        =       (  Button  )   findViewById        (  R.id.         water  )         ;
    snooker      =       (  Button  )   findViewById        (  R.id.       snooker  )         ;
    billiards    =       (  Button  )   findViewById        (  R.id.     billiards  )         ;
    qallayat     =       (  Button  )   findViewById        (  R.id.      qallayat  )         ;
    hookah       =       (  Button  )   findViewById        (  R.id.        hookah  )         ;
    total        =       ( TextView )   findViewById        (  R.id.         total  )         ;


    Bundle cdextras = getIntent().getExtras();
    Bundle hdextras = getIntent().getExtras();
    Double imfromcd=cdextras.getDouble("cdtot");
    Double imfromhd=hdextras.getDouble("hdtot");


}

public void cDrinks (View v)
{
    Intent cd= new Intent(getApplicationContext(),ColdDrinks.class);

    startActivity(cd);
}

public void hDrinks (View v)
{

    Intent hd = new Intent(getApplicationContext(),HotDrinks.class);

    startActivity(hd);

}

public void totcalc (View v)
{

    Bundle cdextras = getIntent().getExtras();

    Double imfromcd=cdextras.getDouble("cdtot");

    total.setText(String.valueOf(imfromcd));

}

public void hdtotcalc (View v)
{

    Bundle hdextras = getIntent().getExtras();

    Double imfromhd=hdextras.getDouble("hdtot");

    total.setText(String.valueOf(imfromhd));

}

@Override
public void onBackPressed() {
    finish();
    super.onBackPressed();
}

}

2- ColdDrinks活动:

public class ColdDrinks extends AppCompatActivity {

Button         cdsave   ;
EditText       epepsi   ;
EditText       eseven   ;
EditText     emiranda   ;
EditText         edew   ;
EditText        ecola   ;
EditText       evimto   ;
EditText    ebarbican   ;
EditText       ezakey   ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cold_drinks);

    cdsave      =   ( Button )  findViewById    (R.id.     cdsave       )         ;
    epepsi      =   (EditText)  findViewById    (R.id.     epepsi       )         ;
    eseven      =   (EditText)  findViewById    (R.id.     eseven       )         ;
    emiranda    =   (EditText)  findViewById    (R.id.   emiranda       )         ;
    edew        =   (EditText)  findViewById    (R.id.       edew       )         ;
    ecola       =   (EditText)  findViewById    (R.id.      ecola       )         ;
    evimto      =   (EditText)  findViewById    (R.id.     evimto       )         ;
    ebarbican   =   (EditText)  findViewById    (R.id.  ebarbican       )         ;
    ezakey      =   (EditText)  findViewById    (R.id.     ezakey       )         ;




}



public void cdsave (View v)
{

    Double dpepsi = Double.parseDouble(epepsi.getText().toString());
    Double calcpepsi = (dpepsi)*0.30;

    Double dseven = Double.parseDouble(eseven.getText().toString());
    Double calcseven = (dseven)*0.30;

    Double dmiranda = Double.parseDouble(emiranda.getText().toString());
    Double calcmiranda = (dmiranda)*0.30;

    Double ddew = Double.parseDouble(edew.getText().toString());
    Double calcdew = (ddew)*0.30;

    Double dcola = Double.parseDouble(ecola.getText().toString());
    Double calccola = (dcola)*0.30;

    Double dvimto = Double.parseDouble(evimto.getText().toString());
    Double calcvimto = (dvimto)*0.45;

    Double dbarbican = Double.parseDouble(ebarbican.getText().toString());
    Double calcbarbican = (dbarbican)*0.40;

    Double dzakey = Double.parseDouble(ezakey.getText().toString());
    Double calczakey = (dzakey)*0.20;


    Double cdtotal = (calcpepsi)+(calcseven)+(calcmiranda)+(calcdew)+(calccola)+(calcvimto)+(calcbarbican)+(calczakey);

    Intent cdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    cdtoim.putExtra("cdtot",cdtotal);

    startActivity(cdtoim);


}

@Override
public void onBackPressed() {

    Double dpepsi = Double.parseDouble(epepsi.getText().toString());
    Double calcpepsi = (dpepsi)*0.30;

    Double dseven = Double.parseDouble(eseven.getText().toString());
    Double calcseven = (dseven)*0.30;

    Double dmiranda = Double.parseDouble(emiranda.getText().toString());
    Double calcmiranda = (dmiranda)*0.30;

    Double ddew = Double.parseDouble(edew.getText().toString());
    Double calcdew = (ddew)*0.30;

    Double dcola = Double.parseDouble(ecola.getText().toString());
    Double calccola = (dcola)*0.30;

    Double dvimto = Double.parseDouble(evimto.getText().toString());
    Double calcvimto = (dvimto)*0.45;

    Double dbarbican = Double.parseDouble(ebarbican.getText().toString());
    Double calcbarbican = (dbarbican)*0.40;

    Double dzakey = Double.parseDouble(ezakey.getText().toString());
    Double calczakey = (dzakey)*0.20;


    Double cdtotal = (calcpepsi)+(calcseven)+(calcmiranda)+(calcdew)+(calccola)+(calcvimto)+(calcbarbican)+(calczakey);

    Intent cdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    cdtoim.putExtra("cdtot",cdtotal);

    startActivity(cdtoim);

    super.onBackPressed();
}

}

3- HotDrinks活动:

public class HotDrinks extends AppCompatActivity {

EditText ecoffee;
EditText enescafe;
EditText ecappuccino;
EditText etea;
EditText ezhorat;
EditText eteawmilk;
EditText eteawyansoun;
Button   hdsave;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hot_drinks);

    ecoffee        =    ( EditText )    findViewById    (R.id.             ecoffee      )     ;
    enescafe       =    ( EditText )    findViewById    (R.id.            enescafe      )     ;
    ecappuccino    =    ( EditText )    findViewById    (R.id.         ecappuccino      )     ;
    etea           =    ( EditText )    findViewById    (R.id.                etea      )     ;
    ezhorat        =    ( EditText )    findViewById    (R.id.             ezhorat      )     ;
    eteawmilk      =    ( EditText )    findViewById    (R.id.           eteawmilk      )     ;
    eteawyansoun   =    ( EditText )    findViewById    (R.id.        eteawyansoun      )     ;
    hdsave         =    (  Button  )    findViewById    (R.id.              hdsave      )     ;

}

public void hdsave (View v)
{
    Double dcoffee = Double.parseDouble(ecoffee.getText().toString());
    Double calccoffee = (dcoffee)*0.50;

    Double dnescafe = Double.parseDouble(enescafe.getText().toString());
    Double calcnescafe = (dnescafe)*0.50;

    Double dcappuccino = Double.parseDouble(ecappuccino.getText().toString());
    Double calccappuccino = (dcappuccino)*0.50;

    Double dtea = Double.parseDouble(etea.getText().toString());
    Double calctea = (dtea)*0.25;

    Double dzhorat = Double.parseDouble(ezhorat.getText().toString());
    Double calczhorat = (dzhorat)*0.35;

    Double dteawmilk = Double.parseDouble(eteawmilk.getText().toString());
    Double calcteawmilk = (dteawmilk)*0.40;

    Double dteawyansoun = Double.parseDouble(eteawyansoun.getText().toString());
    Double calcteawyansoun = (dteawyansoun)*0.40;

    Double hdtotal = (calccoffee)+(calcnescafe)+(calccappuccino)+(calctea)+(calczhorat)+(calcteawmilk)+(calcteawyansoun);

    Intent hdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    hdtoim.putExtra("hdtot",hdtotal);

    startActivity(hdtoim);


}

@Override
public void onBackPressed() {

    Double dcoffee = Double.parseDouble(ecoffee.getText().toString());
    Double calccoffee = (dcoffee)*0.50;

    Double dnescafe = Double.parseDouble(enescafe.getText().toString());
    Double calcnescafe = (dnescafe)*0.50;

    Double dcappuccino = Double.parseDouble(ecappuccino.getText().toString());
    Double calccappuccino = (dcappuccino)*0.50;

    Double dtea = Double.parseDouble(etea.getText().toString());
    Double calctea = (dtea)*0.25;

    Double dzhorat = Double.parseDouble(ezhorat.getText().toString());
    Double calczhorat = (dzhorat)*0.35;

    Double dteawmilk = Double.parseDouble(eteawmilk.getText().toString());
    Double calcteawmilk = (dteawmilk)*0.40;

    Double dteawyansoun = Double.parseDouble(eteawyansoun.getText().toString());
    Double calcteawyansoun = (dteawyansoun)*0.40;

    Double hdtotal = (calccoffee)+(calcnescafe)+(calccappuccino)+(calctea)+(calczhorat)+(calcteawmilk)+(calcteawyansoun);

    Intent hdtoim = new Intent(getApplicationContext(),ItemMenu.class);

    hdtoim.putExtra("hdtot",hdtotal);

    startActivity(hdtoim);

    super.onBackPressed();
}

}

您应该有一个单独的类,该类可以累积所选项目并跟踪运行总计。 此类为Singleton(整个应用程序中只有一个实例),每个Activity从中读取总计和/或将更改提交到所选项目。

您的MenuActivity只要恢复就可以简单地检查当前总数。

您是否尝试过仅实现两个函数的calc函数?

类似于以下内容:

public void totcalc (View v)
{

    Intent intent = getIntent();

    Double imFromHd = intent.getDoubleExtra("hdtot", defaultValue); #defaults are always good
    Double imFromCd = intent.getDoubleExtra("cdtot", defaultValue);
    Double totalCost = imFromHd + imFromCd;

    totalCost += Double.valueOf(total.getText());

    total.setText(total.String.valueOf(totalCost));

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM