gpt4 book ai didi

java - Android 增量计数器不会重置?

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:48 26 4
gpt4 key购买 nike

我正在尝试为 GAA 比赛(爱尔兰足球)创建得分跟踪器。

我已经让它工作了,但每次我运行该应用程序时,它都不会将计数器重置回 0,而是继续递增。

这是我的 MainActivity java 文件中的代码:

public class MainActivity extends ActionBarActivity {

public static int homeGoalsCounter = 0;
public static int homePointsCounter = 0;
public static int awayGoalsCounter = 0;
public static int awayPointsCounter = 0;

Button homeGoalButton;
Button homePointButton;
Button awayGoalButton;
Button awayPointButton;
TextView homeGoalsTextView;
TextView homePointsTextView;
TextView awayGoalsTextView;
TextView awayPointsTextView;

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

homeGoalButton = (Button)(findViewById(R.id.homeGoal));
homePointButton = (Button)(findViewById(R.id.homePoint));
awayGoalButton = (Button)(findViewById(R.id.awayGoal));
awayPointButton = (Button)(findViewById(R.id.awayPoint));
homeGoalsTextView = (TextView)(findViewById(R.id.homeGoals));
homePointsTextView = (TextView)(findViewById(R.id.homePoints));
awayGoalsTextView = (TextView)(findViewById(R.id.awayGoals));
awayPointsTextView = (TextView)(findViewById(R.id.awayPoints));

homeGoalButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){
homeGoalsCounter++;
homeGoalsTextView.setText(Integer.toString(homeGoalsCounter));
}

});

homePointButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
homePointsCounter++;
homePointsTextView.setText(Integer.toString(homePointsCounter));
}
});

awayGoalButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
awayGoalsCounter++;
awayGoalsTextView.setText(Integer.toString(awayGoalsCounter));
}
});

awayPointButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
awayPointsCounter++;
awayPointsTextView.setText(Integer.toString(awayPointsCounter));
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

}

最佳答案

将初始化放入onCreate方法

 public class MainActivity extends ActionBarActivity {

public static int homeGoalsCounter ;
public static int homePointsCounter;
public static int awayGoalsCounter;
public static int awayPointsCounter;


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

homeGoalsCounter = 0;
homePointsCounter = 0;
awayGoalsCounter = 0;
awayPointsCounter = 0;

}
}

关于java - Android 增量计数器不会重置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27489692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com