我的代码有一个问题,我可以使用一些帮助。我制作了一个跟踪足球比赛统计数据的应用程序。应用程序工作正常,问题是当我旋转手机屏幕时出现2个问题: 布局与屏幕宽度不匹配,使文本和按钮相互重叠,当我转动手机时,由于某种原因,分数重置。我有一个重置按钮,但我不知道为什么它在我旋转屏幕时激活。会感激一些帮助。谢谢阅读! 这是XML和Java中的代码:
public class MainActivity extends AppCompatActivity {
int golosA = 0;
int faltasA = 0;
int forasDeJogoA = 0;
int cartoesAmarelosA = 0;
int expulsoesA = 0;
int golosB = 0;
int faltasB = 0;
int forasDeJogoB = 0;
int cartoesAmarelosB = 0;
int expulsoesB = 0;
int cantosA = 0 ;
int cantosB = 0 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void resetScore(View v) {
golosA=0;
faltasA=0;
expulsoesA=0;
cartoesAmarelosA = 0;
forasDeJogoA = 0 ;
golosB=0;
faltasB=0;
expulsoesB=0;
cartoesAmarelosB = 0;
forasDeJogoB = 0 ;
cantosA = 0 ;
cantosB = 0 ;
displayFaltasEquipaA(0);
displayGolosButtonA(0);
displayExpulsoes(0);
displayCartoesA(0);
displayForasJogoA(0);
displayCartoesB(0);
displayExpulsoesB(0);
displayForasJogoB(0);
displayGolosButtonB(0);
displayFaltasEquipaB(0);
displayCantosA(0);
displayCantosB(0);
}
public void displayGolosButtonA(int score) {
TextView scoreView = (TextView) findViewById(R.id.golos_equipa_a);
scoreView.setText(String.valueOf(score));
}
public void golosTeamA(View view) {
golosA = golosA + 1;
displayGolosButtonA(golosA);
}
public void displayFaltasEquipaA(int score) {
TextView scoreView = (TextView) findViewById(R.id.faltas_equipa_a);
scoreView.setText(String.valueOf(score));
}
public void faltasTeamA(View view) {
faltasA = faltasA + 1;
displayFaltasEquipaA(faltasA);
}
public void displayCantosA(int score) {
TextView scoreView = (TextView) findViewById(R.id.cantos_teamA);
scoreView.setText(String.valueOf(score));
}
public void cantosA(View view) {
cantosA = cantosA + 1;
displayCantosA(cantosA);
}
public void displayCantosB(int score) {
TextView scoreView = (TextView) findViewById(R.id.cantos_teamB);
scoreView.setText(String.valueOf(score));
}
public void cantosB(View view) {
cantosB = cantosB + 1;
displayCantosB(cantosB);
}
public void displayForasJogoA(int score) {
TextView scoreView = (TextView) findViewById(R.id.foras_equipa_a);
scoreView.setText(String.valueOf(score));
}
public void forasEquipaA(View view) {
forasDeJogoA = forasDeJogoA + 1;
displayForasJogoA(forasDeJogoA + 1);
}
public void displayCartoesA(int score) {
TextView scoreView = (TextView) findViewById(R.id.c_amarelos_a);
scoreView.setText(String.valueOf(score));
}
public void amarelosTeamA(View view) {
cartoesAmarelosA = cartoesAmarelosA + 1;
displayCartoesA(cartoesAmarelosA);
}
public void displayExpulsoes(int score) {
TextView scoreView = (TextView) findViewById(R.id.expulsoes_a);
scoreView.setText(String.valueOf(score));
}
public void expulsoesTeamA(View view) {
expulsoesA = expulsoesA + 1;
displayExpulsoes(expulsoesA);
}
public void displayGolosButtonB(int score) {
TextView scoreView = (TextView) findViewById(R.id.golos_equipa_b);
scoreView.setText(String.valueOf(score));
}
public void golosTeamB(View view) {
golosB = golosB + 1;
displayGolosButtonB(golosB);
}
public void displayFaltasEquipaB(int score) {
TextView scoreView = (TextView) findViewById(R.id.faltas_equipa_b);
scoreView.setText(String.valueOf(score));
}
public void faltasTeamB(View view) {
faltasB = faltasB + 1;
displayFaltasEquipaB(faltasB);
}
public void displayForasJogoB(int score) {
TextView scoreView = (TextView) findViewById(R.id.foras_equipa_b);
scoreView.setText(String.valueOf(score));
}
public void forasJogoB(View view) {
forasDeJogoB = forasDeJogoB + 1;
displayForasJogoB(forasDeJogoB + 1);
}
public void displayCartoesB(int score) {
TextView scoreView = (TextView) findViewById(R.id.c_amarelos_b);
scoreView.setText(String.valueOf(score));
}
public void amarelosTeamB(View view) {
cartoesAmarelosB = cartoesAmarelosB + 1;
displayCartoesB(cartoesAmarelosB);
}
public void displayExpulsoesB(int score) {
TextView scoreView = (TextView) findViewById(R.id.expulsoes_b);
scoreView.setText(String.valueOf(score));
}
public void expulsoesB(View v) {
expulsoesB = expulsoesB + 1;
displayExpulsoesB(expulsoesB);
}
}
答案 0 :(得分:0)
我不是100%肯定,但我的直觉告诉我你需要添加一个onOrientationChanged监听器,并在水平/横向模式下再次将文本设置为相同的值。
它与活动生命周期有关,当方向发生变化时,如果您不保存这些值或设置值,它会暂时停止/暂停(不确定哪个)活动,然后将它们设置为默认值在你的代码中像0一样。
答案 1 :(得分:0)
有关分数重置问题,请尝试添加以下行:
<activity android:name=".YourActivity"
android:configChanges="orientation|screenSize" >
进入您的清单文件。它告诉您的活动不对屏幕方向更改做出反应。它仍然会旋转,但会阻止变量重置。
对于旋转问题,您是否尝试过编辑应用的旋转版本?我不知道您正在使用哪种编辑器,但在Android Studio的“设计”页面中,您可以选择创建横向版本并手动更改其设计。