英文:
Java Android Studio: value changes when I flip the screen
问题
我正试图创建一个问答游戏,但每当我旋转屏幕时,我会遇到以下问题:
- 当我将屏幕旋转并变为横向时,即使我选择了错误的答案,我也会得到错误的消息,但分数仍然会增加。
- 问题和选项会在旋转屏幕时发生变化,而不是保持不变。
以下是您的代码:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class quiz extends AppCompatActivity {
// ...(省略了一些导入和变量声明)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
// ...(省略了一些视图初始化和监听器设置代码)
}
private void updateQuestion(int nextInt) {
// ...(省略了问题更新逻辑的代码)
}
private void GameOver() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(quiz.this);
alertDialogBuilder.setMessage("Game Over! Your Score is " + mScore).setCancelable(false).setPositiveButton(
"NEW GAME", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(), quiz.class));
}
}).setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Sscore, mScore);
outState.putInt(nums, num);
outstate.putIntegerArrayList(arr, list);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mScore = savedInstanceState.getInt(Sscore);
num = savedInstanceState.getInt(nums);
score.setText("Score: " + mScore);
list.addAll(savedInstanceState.getIntegerArrayList(arr));
}
}
请注意,由于您只要求进行翻译,我已经省略了代码中的注释和部分代码逻辑。如果您有其他问题或需要更多帮助,请随时提问。
英文:
So I am trying to create a quiz game however, whenever I flip my screen around I come across the following issues:
- When I rotate the screen and it changes to landscape, even if I choose the wrong answer, I get the wrong message but the score still gets incremented.
- The question and choices changes rather than staying the same whenever I turn the screen.
here is my code:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class quiz extends AppCompatActivity {
Button answer1, answer2, answer3, answer4;
TextView score,question;
int count=0;
String text="Wrong";
private question mq= new question();
private String manswer;
private int mScore=0;
private int mquestionLength= mq.nquestion.length;
Random r;
private static final String Sscore="";
private static final String nums="";
private String Ques;
private int num;
ArrayList<Integer> list = new ArrayList<Integer>();
private String arr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quiz);
num=0;
r = new Random();
answer1 = (Button) findViewById(R.id.answer1);
answer2 = (Button) findViewById(R.id.answer2);
answer3 = (Button) findViewById(R.id.answer3);
answer4 = (Button) findViewById(R.id.answer4);
score = (TextView) findViewById(R.id.score);
question = (TextView) findViewById(R.id.question);
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
answer1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer1.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
}
else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
answer2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer2.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
} else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
answer3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer3.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
} else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
answer4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (answer4.getText() == manswer) {
mScore++;
score.setText("Score: " + mScore);
updateQuestion(r.nextInt(mquestionLength));
} else {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
updateQuestion(r.nextInt(mquestionLength));
}
if(num>8){
GameOver();
}
}
}
);
}
private void updateQuestion ( int nextInt){
if(count==0) {
question.setText(mq.getQuestion(nextInt));
answer1.setText(mq.getChoice(nextInt));
answer2.setText(mq.getChoice2(nextInt));
answer3.setText(mq.getChoice3(nextInt));
answer4.setText(mq.getChoice4(nextInt));
manswer = mq.getAnswer(nextInt);
}
else{
if(!list.contains(nextInt)){
question.setText(mq.getQuestion(nextInt));
answer1.setText(mq.getChoice(nextInt));
answer2.setText(mq.getChoice2(nextInt));
answer3.setText(mq.getChoice3(nextInt));
answer4.setText(mq.getChoice4(nextInt));
manswer = mq.getAnswer(nextInt);
}
else{
updateQuestion(r.nextInt(mquestionLength));
}
}
list.add(nextInt);
count++;
}
private void GameOver() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(quiz.this);
alertDialogBuilder.setMessage("Game Over! Your Score is "+mScore).setCancelable(false).setPositiveButton(
"NEW GAME", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getApplicationContext(),quiz.class));
}
}).setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog=alertDialogBuilder.create();
alertDialog.show();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(Sscore,mScore);
outState.putInt(nums,num);
outstate.putIntegerArrayList(arr,list);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mScore=savedInstanceState.getInt(Sscore);
num=savedInstanceState.getInt(nums);
score.setText("Score: "+mScore);
list.addAll(savedInstanceState.getIntegerArrayList(arr));
}
}
答案1
得分: 0
安卓应用的默认行为是,当你旋转屏幕并且没有设置唯一的方向(横向或纵向),实际的活动会被销毁然后重新创建。
如果你想避免这种情况,你有两个选项:
-
你可以在清单文件中的活动部分添加以下属性:
android:configChanges="orientation|screenSize"
-
你可以通过以下方式设置活动或整个应用的方向:
android:screenOrientation="portrait"
或者
android:screenOrientation="landscape"
英文:
The default behaviour of the android's apps is when you rotate the screen and you don't set a unice orientation(landscape or portrait) the actual activity is destroyed and recreated again.
You have 2 options if you want to avoid this:
-
you can add on the manifest on the activity the following attribute :
android:configChanges="orientation|screenSize"
-
You can set the orientation of the activity or the all the app with this:
android:screenOrientation="portrait"
or
android:screenOrientation="landscape"
答案2
得分: 0
你的代码表现出乎意料,因为你在生命周期中使用了 onRestoreInstanceState
,而它在 onCreate
之后很长一段时间才被调用。但是你已经在 onCreate
中执行了所有重要的操作。据我所知,onRestoreInstanceState
在 onStart
之后被调用。
你有两个选项:
1)在一个私有方法中执行你的操作,并且从 onCreate
和 onRestoreInstanceState
中调用它(不太理想)。
2)你可以直接在 onCreate
中使用 savedInstanceState
参数。它包含了你在 onSaveInstanceState
回调中保存的 Bundle。(更好)
protected void onSaveInstanceState(Bundle instanceState) {
super.onSaveInstanceState(instanceState);
instanceState.putString("your_first_value", value1);
instanceState.putString("your_second_value", value2);
// ... 保存所有你想要保留的变量 ...
}
然后,稍后当活动被重新创建时,你可以在 `onCreate` 中得到刚刚保存的 Bundle,它会作为 `savedInstanceState` 参数被传入:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(...); // 这创建了你的控件
// 如果 savedInstanceState 不为 null,说明你有一个已保存的状态在这里...
// 所以你可以恢复它
if (savedInstanceState != null) {
value1 = savedInstanceState.getString("your_first_value");
value2 = savedInstanceState.getString("your_second_value");
// ... 在这里恢复所有的值 ...
}
}
英文:
Your code behaves unexpected because you use onRestoreInstanceState
which is called long after onCreate
in the lifecycle.
But you do all your important stuff already in onCreate
.
As far as I know, onRestoreInstanceState
is called after onStart.
You have two options:
-
do your stuff in a private method that you call from
onCreate
andonRestoreInstanceState
(not-so-good) -
you can simply use the
savedInstanceState
parameter directly inonCreate
. It contains the bundle you saved in theonSaveInstanceState
callback. (better)protected void onSaveInstanceState(Bundle instanceState) {
super.onSaveInstanceState(instanceState);
instanceState.putString("your_first_value", value1);
instanceState.putString("your_second_value", value2);
// ... save all your variables you want to keep ...
}
Then, later, when the activity is re-created you get this bundle you just saved supplied in the savedInstanceState
parameter:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(...); // This creates your controls
// if savedInstanceState is not null, you have a saved state here...
// so you can restore it
if (savedInstanceState != null) {
value1 = savedInstanceState.getString("your_first_value");
value2 = savedInstanceState.getString("your_second_value");
// ... here you restore all your values ...
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论