从两个不同的活动中添加两个值,并在不同的活动中显示总计

时间:2016-08-17 19:27:05

标签: java android android-studio

我有3项活动:1- ItemMenu,2-Snooker,3-Billiards

当我按下ItemMenu活动中的按钮来计算斯诺克&的总数时。台球活动,它只是给了我去过的最后一项活动的总数,我需要它给我总共2项活动合并

以下是Billiards Activity的代码:

public class Billiards extends AppCompatActivity {

EditText ebgames;
Button   bsave;

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

    ebgames = (EditText)findViewById(R.id.ebgames);
    bsave   = ( Button )findViewById(R.id.  bsave);

}

public void bsave (View v)
{

    Double dbgames = Double.parseDouble(ebgames.getText().toString());
    Double calcbgames = (dbgames)*0.50;

    Double btotal = (calcbgames);


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

    btoim.putExtra("btot",btotal);

    startActivity(btoim);

}

以下是斯诺克活动的代码:

public class Snooker extends AppCompatActivity {

EditText esgames;
Button   ssave;


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

    esgames = (EditText)findViewById(R.id.esgames);
    ssave   = ( Button )findViewById(R.id.  ssave);



}

public void ssave (View v)
{

    Double dsgames = Double.parseDouble(esgames.getText().toString());
    Double calcsgames = (dsgames)*1.00;

    Double stotal = (calcsgames);


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

    stoim.putExtra("stot",stotal);

    startActivity(stoim);

}

以下是ItemMenu活动的代码:

public class ItemMenu extends AppCompatActivity {

Button snooker;
Button billiards;
TextView total;

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


    snooker             =       (  Button  )   findViewById        (  R.id.              snooker  )         ;
    billiards           =       (  Button  )   findViewById        (  R.id.            billiards  )         ;


}


public void billiards (View v)
{

    Intent billiards = new Intent(getApplicationContext(),Billiards.class);

    startActivity(billiards);

}

public void snooker (View v)
{

    Intent snooker = new Intent(getApplicationContext(),Snooker.class);

    startActivity(snooker);

}

public void totcalc (View v)
{

    Intent gettot = getIntent();

    Double imfromb = gettot.getDoubleExtra("btot",0);
    Double imfroms = gettot.getDoubleExtra("stot",0);

    Double gtotal =  imfromb + imfroms;

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

}

2 个答案:

答案 0 :(得分:0)

在您当前的版本中,每次onCreate()开始时,您应该在ItemMenu获得最新分数。然后,您可以将其添加到列表中或保持运行总计。

或者,您可以使用Snooker启动BilliardsstartActivityForResult()。与我之前的建议类似,您可以在onActivityResult()中获得分数,然后保存分数列表,保持总计或任何您想要的分数。

有关从活动返回结果的详细信息,请参阅Getting a Result from an ActivityStarting Activities and Getting Results

答案 1 :(得分:-1)

您可以在SharedPreferences中保存btotalstotal值,并从ItemMenu活动中访问它。