我不敢相信我有这么多麻烦。我在游戏活动中有这个变量:
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并对该变量做同样的事情,然后总计这两个变量并设置它成功的textView?
菜单活动:
public class Izbor extends 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;
}
}
}
答案 0 :(得分: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);
}
}
编辑:尝试帮助使事情变得更清晰的一些变化
答案 1 :(得分: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);
}
}