我目前正在 Android Studio 中制作一个标记测验应用程序,我想添加一个功能,每个正确答案给你 1 分,错误答案给你 0 分。(按提示按钮或跳过按钮也会给你0)。最后,您可以按一个名为“获取我的分数”的按钮,其中会给出最终分数。
我的问题是,即使我提交正确的答案,我的分数也是 0,而当我提交错误的答案时,有时我的分数是正数......也就是出了问题。我是否遗漏了一些代码,或者我只是以错误的方式完成了此操作?
更新
成功了!正确的代码如下。
首先,这是主要 Activity ,包含所有 fragment (标志)的代码:
Play.java
public class Play extends MainActivity {
private int total = 0;
public void updateScore(int add){
total += add;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
final String myScore = String.valueOf(total);
final ImageButton imageBtn10 = (ImageButton) findViewById(R.id.imageButton10);
final ImageButton imageBtn9 = (ImageButton) findViewById(R.id.imageButton9);
final ImageButton imageBtn8 = (ImageButton) findViewById(R.id.imageButton8);
final ImageButton imageBtn7 = (ImageButton) findViewById(R.id.imageButton7);
final ImageButton imageBtn6 = (ImageButton) findViewById(R.id.imageButton6);
final ImageButton imageBtn5 = (ImageButton) findViewById(R.id.imageButton5);
final ImageButton imageBtn4 = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton imageBtn3 = (ImageButton) findViewById(R.id.imageButton3);
final Button button_score = (Button)findViewById(R.id.scoreButton);
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
FragmentDefault fd = new FragmentDefault();
fragTrans.replace(R.id.fragment_place, fd);
fragTrans.commit();
fragTrans.hide(new FragmentOne());
fragTrans.hide(new FragmentTwo());
fragTrans.hide(new FragmentThree());
button_score.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle = new Bundle();
bundle.putString("yes", String.valueOf(total));
Intent intent= new Intent(Play.this, FinalActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
imageBtn10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentOne());
fragTrans.commit();
//imageBtn10.setEnabled(false);
}
});
imageBtn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentTwo());
fragTrans.commit();
}
});
imageBtn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentThree());
fragTrans.commit();
}
});
imageBtn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentThree());
fragTrans.commit();
}
});
imageBtn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentThree());
fragTrans.commit();
}
});
imageBtn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentThree());
fragTrans.commit();
}
});
imageBtn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentThree());
fragTrans.commit();
}
});
imageBtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragMan = getFragmentManager();
FragmentTransaction fragTrans = fragMan.beginTransaction();
fragTrans.replace(R.id.fragment_place, new FragmentThree());
fragTrans.commit();
}
});
}
}
第一个标志( fragment )的代码:
FragmentOne.java
public class FragmentOne extends Fragment {
private EditText userAnswer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_fragment_one, null);
userAnswer = (EditText)v.findViewById(R.id.editText);
final TextView tv = (TextView)v.findViewById(R.id.showCorrect);
final TextView hintv = (TextView)v.findViewById(R.id.textHint);
final Button submit = (Button)v.findViewById(R.id.submitBtn1);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String theAnswer = (userAnswer.getText().toString());
if (theAnswer.equalsIgnoreCase("Germany")) {
((Play) getActivity()).updateScore(1);
tv.setText("Correct!");
}
else {
tv.setText("Wrong");
}
submit.setEnabled(false);
// updateScore();
}
});
final Button skip = (Button)v.findViewById(R.id.skipBtn);
skip.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submit.setEnabled(false);
}
});
final Button hint = (Button)v.findViewById(R.id.hintBtn);
hint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
hintv.setText("The capital is Berlin \n The country is in Europe \n It starts with G... ");
}
});
return v;}
}
这是其中一个问题的代码。因此,您可以单击国旗并写下属于哪个国家/地区。每个标志都是一个 fragment 。
由于这一行,我在上面的代码中收到错误: ((播放) getActivity()).updateScore(1); Play 颜色为红色,并显示“无法解析符号“Play””。
这是当您按下“获取我的分数”按钮时的代码:
FinalActivity.java
public class FinalActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
Bundle bundle = getIntent().getExtras();
final String yes_sir = bundle.getString("yes"); //yes_sir has to be the total score added up.
final TextView tv2 = (TextView)findViewById(R.id.textView5);
final Button submit = (Button)findViewById(R.id.button4);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
tv2.setText("Score is " + yes_sir);
}
});
}
}
我是一个完全的编程初学者,所以详细的解释会非常有帮助!
最佳答案
您可以使用“yes”键从 bundle 中获取分数:
final String yes_sir = bundle.getString("yes");
我猜你的分数应该是“a”,因为当答案正确时,你的分数会增加。您没有向我们展示代码,您可以在其中将 View 更改为分数 View (创建新的 Activity ),也可以在其中将分数添加到 bundle 中。您应该将分数添加到 fragment 类中的 bundle 中,如下所示:
bundle.putString("yes", String.valueOf(a));
您可以使用 Android Bundles 将数据提供给新的 Activity。为此,您可以将数据放入 Bundle 中,并将其提供给新 Activity 的 onCreate。在那里您可以取回数据。您的问题是,您尝试从 Bundle 中获取数据,但从未向其中添加数据。
为了帮助您了解更详细的信息,查看代码会很有用,您可以在其中创建显示分数的 Activity 。
您需要使用 Intent 将该 Bundle 提供给您的新 Activity。
当你想启动 FinalActivity 时,你应该这样做:
Intent intent= new Intent(this, FinalActivity.class);
intent.putExtras(bundle);
startActivity(intent);
要在 Fragments 及其 Activity 之间进行通信,最简单(但不是最好)的方法如下:
((Play) getActivity()).updateScore(1);
在您的 fragment 中执行此操作,而不是公共(public)静态 int。所以而不是你的“a++;”你写上面的行。
在您的 Play 类中,您必须添加以下功能:
public void updateScore(int add){
total += add;
}
你应该改变:
final int total = (0 + FragmentOne.a + FragmentTwo.b);
致: 私有(private) int 总数 = 0;
并将其从 onCreate() 函数移动到类中。
而不是:
bundle.putString("yes", myScore);
您现在可以执行以下操作:
bundle.putString("yes", String.valueOf(total));
注意:出现您的问题是因为您的分数在您创建 Play Activity 时初始化过一次。这意味着,当您第一次初始化它时,总计将设置为 0。当您现在正确回答问题时,总计不会更新,因为它仅在您初始化 Activity 时设置。当您按下“分数”按钮时,它仍然是 0。当您现在切换回“游戏 Activity ”时,总数会再次初始化并变为 1(如果您之前回答正确)。
关于java - Android Studio 带有分数的测验应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41519918/