防止用户输入多个答案

huangapple go评论93阅读模式
英文:

preventing a user from entering multiple answer

问题

  1. Button btrue,bfalse,bnext,bprevious;
  2. TextView tquestion;
  3. int mCurrentIndex=0;
  4. Questions[] questionbank;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. btrue=findViewById(R.id.true_button);
  10. bfalse=findViewById(R.id.false_button);
  11. bnext=findViewById(R.id.next_button);
  12. tquestion=findViewById(R.id.question_text);
  13. bprevious=findViewById(R.id.previous_button);
  14. questionbank = new Questions[] {
  15. new Questions(R.string.greater,false),
  16. new Questions(R.string.Pm,true),
  17. new Questions(R.string.capital,true),
  18. new Questions(R.string.china,false),
  19. new Questions(R.string.Richer,false),
  20. new Questions(R.string.company,true),
  21. new Questions(R.string.company1,false),
  22. };
  23. update();
  24. bnext.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View view) {
  27. int s=questionbank.length;
  28. if( mCurrentIndex<=s ) {
  29. if (mCurrentIndex + 1 < questionbank.length) {
  30. mCurrentIndex++;
  31. } else {
  32. Toast.makeText(getApplicationContext(),"not more question",Toast.LENGTH_SHORT).show();
  33. }
  34. update();
  35. }
  36. }
  37. });
  38. btrue.setOnClickListener(new View.OnClickListener() {
  39. @Override
  40. public void onClick(View view) {
  41. checkAnswer(true);
  42. }
  43. });
  44. bfalse.setOnClickListener(new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. checkAnswer(false);
  48. }
  49. });
  50. bprevious.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View view) {
  53. if (mCurrentIndex > 0) {
  54. mCurrentIndex = (mCurrentIndex - 1);
  55. update();
  56. } else {
  57. Toast.makeText(getApplicationContext(), "cant go back", Toast.LENGTH_SHORT).show();
  58. }
  59. }
  60. });
  61. }
  62. public void update() {
  63. int ques = questionbank[mCurrentIndex].getmTextResid();
  64. tquestion.setText(ques);
  65. setButtonEnabled(true);
  66. }
  67. private void checkAnswer(boolean userPressedTrue) {
  68. boolean answerIsTrue = questionbank[mCurrentIndex].ismTrueAnswer();
  69. int messageResId = 0;
  70. if (userPressedTrue == answerIsTrue) {
  71. setButtonEnabled(false);
  72. messageResId = R.string.correct_toast;
  73. } else {
  74. setButtonEnabled(false);
  75. messageResId = R.string.incorrect_toast;
  76. }
  77. Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
  78. }
  79. private void setButtonEnabled(boolean enabled) {
  80. btrue.setEnabled(enabled);
  81. bfalse.setEnabled(enabled);
  82. }
英文:

i create a true/false quiz app. in that app i have 4 buttons i.e True,False,previous(to get back to previous question),next(to get the next question). i want to add a functionality when a user click on true or false button its get disable for that question so the user can click only once for one question.

i tried to disable the button by using button.setEnabled(false) but when i click on previous button or next button the true/false buttons enabled.i want that the user can click the answer only once it does not matter how much the user traverse the question.

i try to call the setEnabledButton() in previous on click button but it disable the button but it also disable whether the user click on answer or not.

  1. Button btrue,bfalse,bnext,bprevious;
  2. TextView tquestion;
  3. int mCurrentIndex=0;
  4. Questions[] questionbank;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. btrue=findViewById(R.id.true_button);
  10. bfalse=findViewById(R.id.false_button);
  11. bnext=findViewById(R.id.next_button);
  12. tquestion=findViewById(R.id.question_text);
  13. bprevious=findViewById(R.id.previous_button);
  14. questionbank = new Questions[]
  15. {
  16. new Questions(R.string.greater,false),
  17. new Questions(R.string.Pm,true),
  18. new Questions(R.string.capital,true),
  19. new Questions(R.string.china,false),
  20. new Questions(R.string.Richer,false),
  21. new Questions(R.string.company,true),
  22. new Questions(R.string.company1,false),
  23. };
  24. update();
  25. bnext.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View view) {
  28. int s=questionbank.length;
  29. if( mCurrentIndex&lt;=s )
  30. {
  31. if (mCurrentIndex + 1 &lt; questionbank.length) {
  32. mCurrentIndex++;
  33. }
  34. else
  35. { Toast.makeText(getApplicationContext(),&quot;not more question&quot;,Toast.LENGTH_SHORT).show();
  36. }
  37. update();
  38. }
  39. }
  40. });
  41. btrue.setOnClickListener(new View.OnClickListener() {
  42. @Override
  43. public void onClick(View view) {
  44. checkAnswer(true);
  45. }
  46. });
  47. bfalse.setOnClickListener(new View.OnClickListener() {
  48. @Override
  49. public void onClick(View view) {
  50. checkAnswer(false);
  51. }
  52. });
  53. bprevious.setOnClickListener(new View.OnClickListener() {
  54. @Override
  55. public void onClick(View view) {
  56. if (mCurrentIndex &gt; 0) {
  57. mCurrentIndex = (mCurrentIndex - 1);
  58. update();
  59. }
  60. else
  61. { Toast.makeText(getApplicationContext(), &quot;cant go back&quot;, Toast.LENGTH_SHORT).show(); }
  62. }
  63. });
  64. }
  65. public void update()
  66. {
  67. int ques = questionbank[mCurrentIndex].getmTextResid();
  68. tquestion.setText(ques);
  69. setButtonEnabled(true);
  70. }
  71. private void checkAnswer(boolean userPressedTrue)
  72. {
  73. boolean answerIsTrue =
  74. questionbank[mCurrentIndex].ismTrueAnswer();
  75. int messageResId = 0;
  76. if (userPressedTrue == answerIsTrue) {
  77. setButtonEnabled(false);
  78. messageResId = R.string.correct_toast;
  79. } else {
  80. setButtonEnabled(false);
  81. messageResId = R.string.incorrect_toast;
  82. }
  83. Toast.makeText(this, messageResId,
  84. Toast.LENGTH_SHORT)
  85. .show();
  86. }
  87. private void setButtonEnabled(boolean enabled) {
  88. btrue.setEnabled(enabled);
  89. bfalse.setEnabled(enabled);
  90. }

}

答案1

得分: 2

在Questions类中,您可以创建一个方法,

  1. public class Questions {
  2. boolean isQuestionAnswered;
  3. public boolean isQuestionAnswered() {
  4. return isQuestionAnswered;
  5. }
  6. public void setQuestionAnswered(boolean questionAnswered) {
  7. isQuestionAnswered = questionAnswered;
  8. }
  9. }

在私有方法中检查答案(checkAnswer),其中userPressedTrue是布尔值参数,

  1. private void checkAnswer(boolean userPressedTrue) {
  2. questionbank[mCurrentIndex].setQuestionAnswered(true);
  3. boolean answerIsTrue = questionbank[mCurrentIndex].ismTrueAnswer();
  4. int messageResId = 0;
  5. if (userPressedTrue == answerIsTrue) {
  6. setButtonEnabled(false);
  7. messageResId = R.string.correct_toast;
  8. } else {
  9. setButtonEnabled(false);
  10. messageResId = R.string.incorrect_toast;
  11. }
  12. Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
  13. }

公共方法update,

  1. public void update() {
  2. int ques = questionbank[mCurrentIndex].getmTextResid();
  3. tquestion.setText(ques);
  4. if(questionbank[mCurrentIndex].isQuestionAnswered())
  5. setButtonEnabled(false);
  6. else
  7. setButtonEnabled(true);
  8. }

请尝试这样做,检查是否解决了您的问题。

英文:

In Questions class you can create a method,

  1. public class Questions {
  2. boolean isQuestionAnswered;
  3. public boolean isQuestionAnswered() {
  4. return isQuestionAnswered;
  5. }
  6. public void setQuestionAnswered(boolean questionAnswered) {
  7. isQuestionAnswered = questionAnswered;
  8. }
  9. }
  1. private void checkAnswer(boolean userPressedTrue)
  2. {
  3. questionbank[mCurrentIndex].setQuestionAnswered(true);
  4. boolean answerIsTrue =
  5. questionbank[mCurrentIndex].ismTrueAnswer();
  6. int messageResId = 0;
  7. if (userPressedTrue == answerIsTrue) {
  8. setButtonEnabled(false);
  9. messageResId = R.string.correct_toast;
  10. } else {
  11. setButtonEnabled(false);
  12. messageResId = R.string.incorrect_toast;
  13. }
  14. Toast.makeText(this, messageResId,
  15. Toast.LENGTH_SHORT)
  16. .show();
  17. }
  1. public void update()
  2. {
  3. int ques = questionbank[mCurrentIndex].getmTextResid();
  4. tquestion.setText(ques);
  5. if(questionbank[mCurrentIndex].isQuestionAnswered())
  6. setButtonEnabled(false);
  7. else
  8. setButtonEnabled(true);
  9. }

Try this out and check whether this solves your problem

huangapple
  • 本文由 发表于 2020年9月9日 18:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63809482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定