简体   繁体   English

如何将int值从一个类传递到另一个类?

[英]How to pass int value from one class to another?

I can't believe I have so much trouble with this. 我不敢相信我有这么多麻烦。 I have this variable in my game activity: 我的游戏活动中包含以下变量:

public static int numberOfPointsA;

and in another activity 在另一项活动中

public static int numberOfPointsB;

Now I need these values to pass to another activity, where I should total these values and set result to textView. 现在,我需要将这些值传递给另一个活动,在这里我应该合计这些值并将结果设置为textView。 Since these are public static variables I tried: 由于这些是公共静态变量,因此我尝试:

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

textView.setText("" + totalScore);

But that's not working. 但这不起作用。 So I tried with intent: 所以我尝试了一下:

In game classA: 在游戏类A中:

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

and in menu class: 并在菜单类中:

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

But that's not working either. 但这也不起作用。 If I place in the scope of activity, as soon as activity starts it crashes. 如果我将活动置于活动范围内,则活动一旦开始就会崩溃。 If I place it in onCreate method, I can's use my int variable anymore, I don't need it in onCreate method, I need it elsewhere. 如果将其放在onCreate方法中,则可以再使用int变量,而在onCreate方法中不需要它,在其他地方也需要它。

So how to set my variable in game class, pass to menu class, save it there and then make it wait until I finish my game class B and do the same with that variable, and then total those two variables and set it successfuly to the textView? 那么如何在游戏类中设置我的变量,传递给菜单类,将其保存在那里,然后使其等待,直到我完成我的游戏类B并对该变量进行相同的操作,然后将这两个变量求和并将其成功设置为textView?

Menu activity: 菜单活动:

public class Izbor extends Activity implements OnClickListener{ 公共类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;
    }

}

} }

The following is a very basic example, using two activities to pass an int around. 以下是一个非常基本的示例,使用两个活动来传递一个int

First Activity, which you start from: 首次活动,您从以下位置开始:

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);
    }
}

And the second Activity example is something like: 第二个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);
    }
}

Edit: Some changes to try help make things clearer 编辑:尝试进行一些更改有助于使事情更清晰

I didn't completely get what you have been trying to say but what I got was you want to create a variable in FirstActivity send its value to SecondActivity and then pass their sum to ResultActivity if its so then here you go 我没有完全理解您要说的内容,但是我想要在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);
    }
}

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

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