0

我不敢相信我有这么多麻烦。我的游戏活动中有这个变量:

public static int numberOfPointsA;

在另一个活动中

public static int numberOfPointsB;

现在我需要将这些值传递给另一个活动,我应该汇总这些值并将结果设置为 textView。由于这些是我尝试的公共静态变量:

public static int totalScore = ClassA.numberOfPointsA + ClassB.numberOfPointsB;

textView.setText("" + totalScore);

但这行不通。所以我故意尝试:

在游戏类A中:

Intent intent = new Intent(this, Menu.class);
                intent.putExtra("foobar", numberOfPointsA);
                startActivity(intent);

在菜单类中:

Intent intent = getIntent();
    int numberOfPointsA = intent.getIntExtra("foobar", 0);

但这也行不通。如果我放在活动范围内,活动一开始它就会崩溃。如果我将它放在 onCreate 方法中,我就可以再使用我的 int 变量了,我在 onCreate 方法中不需要它,我在其他地方需要它。

那么如何在游戏类中设置我的变量,传递给菜单类,将其保存在那里,然后让它等到我完成我的游戏类 B 并对那个变量做同样的事情,然后合计这两个变量并将其成功设置为文本视图?

菜单活动:

公共类 Izbor 扩展 Activity 实现 OnClickListener{

private int asocijacijeUkupno = 0;
private int thUkupno = 0;

int ukupanBrojPoena = asocijacijeUkupno + thUkupno;

Button toploHladno, asocijacije, cigle, spojnice, nazad, poeniTH, poeniAso, poeniCigle, poeniSpojnice, poeniUkupno;
TextView naslov;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    asocijacijeUkupno = getIntent().getIntExtra("RUNNING_TOTAL", 0);
    thUkupno = getIntent().getIntExtra("RUNNING_TOTAL2", 0);

    setContentView(R.layout.izbor);

    addListenerOnButton();

}


private void addListenerOnButton() {

    naslov = (TextView) findViewById(R.id.tvIzborNaslov);
    toploHladno = (Button) findViewById(R.id.bIzbor1);
    asocijacije = (Button) findViewById(R.id.bIzbor2);
    cigle = (Button) findViewById(R.id.bIzbor3);
    spojnice = (Button) findViewById(R.id.bIzbor4);
    nazad = (Button) findViewById(R.id.bIzborNazad);
    poeniTH = (Button) findViewById(R.id.bPoeniTH);
    poeniAso = (Button) findViewById(R.id.bPoeniAso);
    poeniCigle = (Button) findViewById(R.id.bPoeniCigle);
    poeniSpojnice = (Button) findViewById(R.id.bPoeniSpojnice);
    poeniUkupno = (Button) findViewById(R.id.bPoeniUkupno);


    toploHladno.setOnClickListener(this);
    asocijacije.setOnClickListener(this);
    cigle.setOnClickListener(this);
    spojnice.setOnClickListener(this);
    nazad.setOnClickListener(this);

}


@Override
protected void onStart() {
    super.onStart();

    if(asocijacijeUkupno != 0){
    poeniAso.setText("" + asocijacijeUkupno);
    }else{
        poeniAso.setText("");
    }
    if(thUkupno != 0){
    poeniTH.setText("" + thUkupno);
    }else{
        poeniTH.setText("");
    }
    if(ukupanBrojPoena != 0){
    poeniUkupno.setText("" + ukupanBrojPoena);
    }else{
        poeniUkupno.setText("");
    }
}

public void onClick(View v) {
    switch(v.getId()){
    case R.id.bIzbor1:
        if(music == true){
            buttonClicks.start();
                }
        startActivity(new Intent("rs.androidaplikacije.toplo_hladno.TOPLOHLADNO"));
        break;
    case R.id.bIzbor2:
        if(music == true){
            buttonClicks.start();
                }
        startActivity(new Intent("rs.androidaplikacije.toplo_hladno.ASOCIJACIJE"));
        break;
    case R.id.bIzbor3:
        if(music == true){
            buttonClicks.start();
                }

        break;
    case R.id.bIzbor4:
        if(music == true){
            buttonClicks.start();
                }

        break;
    case R.id.bIzborNazad:
        if(music == true){
            buttonBack.start();
                }
        finish();
        break;
    }

}

}

4

2 回答 2

0

我没有完全明白你一直想说的话,但我得到的是你想在 FirstActivity 中创建一个变量,将其值发送给 SecondActivity,然后将它们的总和传递给 ResultActivity,如果是这样,那么你就去吧

//First Activity
public class FirstActivity extends Activity{
    int firstValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        firstValue = 10;
    }
    public sendValueToSecondActivity(View view){
        Intent intent = new Intent(this, SecondActivity.class);
        intent.putExtra("firstValue", firstValue);
        startActivity(intent);
    }
}

//Second Activity
public class SecondActivity extends Activity{
    int sumOfFirstAndSecond;
    int secondValue;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        secondValue = 20;

    }
    public sendValueToSecondActivity(View view){
        sumOfFirstAndSecond = getIntent().getIntExtra("firstValue") + secondValue;
        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("sumOfFirstAndSecond", sumOfFirstAndSecond);
        startActivity(intent);
    }
}

//Result Activity
public class ResultActivity extends Activity{
    TextView textView;
    int result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        secondValue = 20;
        textView = (TextView) findViewById(R.id.textView);
    }
    public setResultToTextView(View view){
        result = getIntent().getIntExtra("sumOfFirstAndSecond");
        textView.setText(""+result);
    }
}
于 2014-11-10T17:39:30.580 回答
0

以下是一个非常基本的示例,使用两个活动来传递int

第一个活动,您从以下内容开始:

public class FirstActivity extends Activity {
    private int mTotal = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        //.... onCreate code, layout stuff etc....    
    }

    // this is tied to a View onClick call
    public void moveToSecondAct(View view) {
        Intent intent = new Intent();
        intent.putExtra("RUNNING_TOTAL",mTotal);
        intent.setClass(this, SecondActivity.class);
        //... any other options like ...
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        // start the second Activity
        startActivity(intent);
    }
}

第二个 Activity 示例类似于:

public class SecondActivity extends Activity {
    private int mTotal = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        mTotal = getIntent().getIntExtra("RUNNING_TOTAL", 0);
        //.... onCreate code, layout stuff etc....

        Log.i("Running total is:"+ mTotal);
        // update the total on the TextView
        TextView tv = (TextView) findViewById(R.id.poeniUkupno);
        tv.setText("Total: "+ mTotal);
    }
}

编辑:尝试进行一些更改以帮助使事情更清晰

于 2013-04-07T02:12:17.530 回答