简体   繁体   English

防止用户输入多个答案

[英]preventing a user from entering multiple answer

i create a true/false quiz app.我创建了一个真/假测验应用程序。 in that app i have 4 buttons ie True,False,previous(to get back to previous question),next(to get the next question).在该应用程序中,我有 4 个按钮,即 True、False、上一个(返回上一个问题)、下一个(获得下一个问题)。 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.我想在用户单击 true 或 false 按钮​​时添加一项功能,该功能将禁用该问题,以便用户只能为一个问题单击一次。

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.我试图通过使用 button.setEnabled(false) 禁用按钮,但是当我单击上一个按钮或下一个按钮时,真/假按钮已启用。我希望用户只能单击一次答案,不管用户有多少遍历问题。

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.我尝试在单击按钮之前调用 setEnabledButton() ,但它禁用了该按钮,但它也禁用了用户是否单击应答。

Button btrue,bfalse,bnext,bprevious;
TextView tquestion;
int mCurrentIndex=0;
Questions[] questionbank;

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

    btrue=findViewById(R.id.true_button);
    bfalse=findViewById(R.id.false_button);
    bnext=findViewById(R.id.next_button);
    tquestion=findViewById(R.id.question_text);
    bprevious=findViewById(R.id.previous_button);


    questionbank = new Questions[]
             {

                new Questions(R.string.greater,false),
                     new Questions(R.string.Pm,true),
                     new Questions(R.string.capital,true),
                     new Questions(R.string.china,false),
                     new Questions(R.string.Richer,false),
                     new Questions(R.string.company,true),
                     new Questions(R.string.company1,false),

             };


         update();

         bnext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            int s=questionbank.length;
            if( mCurrentIndex<=s  )
                {
                    if (mCurrentIndex + 1 < questionbank.length) {
                        mCurrentIndex++;

                    }
                    else
                    {   Toast.makeText(getApplicationContext(),"not more question",Toast.LENGTH_SHORT).show();
                    }
                    update();

            }
        }
    });


    btrue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            checkAnswer(true);

        }
    });

    bfalse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
               checkAnswer(false);
        }
    });

    bprevious.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mCurrentIndex > 0) {
                mCurrentIndex = (mCurrentIndex - 1);
                update();
            }
            else
                { Toast.makeText(getApplicationContext(), "cant go back", Toast.LENGTH_SHORT).show(); }

        }
    });

}

public void update()
{
    int ques = questionbank[mCurrentIndex].getmTextResid();
      tquestion.setText(ques);
      setButtonEnabled(true);

}

private void checkAnswer(boolean userPressedTrue)
{
    boolean answerIsTrue =
            questionbank[mCurrentIndex].ismTrueAnswer();
    int messageResId = 0;
    if (userPressedTrue == answerIsTrue) {
        setButtonEnabled(false);
        messageResId = R.string.correct_toast;
    } else {
        setButtonEnabled(false);
        messageResId = R.string.incorrect_toast;
    }
    Toast.makeText(this, messageResId,
            Toast.LENGTH_SHORT)
            .show();
}

private void setButtonEnabled(boolean enabled) {

    btrue.setEnabled(enabled);
    bfalse.setEnabled(enabled);
}

} }

In Questions class you can create a method,在 Questions 类中,您可以创建一个方法,

public class Questions {

    boolean isQuestionAnswered;

    public boolean isQuestionAnswered() {
        return isQuestionAnswered;
    }

    public void setQuestionAnswered(boolean questionAnswered) {
        isQuestionAnswered = questionAnswered;
    }
}
private void checkAnswer(boolean userPressedTrue)
{
    questionbank[mCurrentIndex].setQuestionAnswered(true);    
    boolean answerIsTrue =
            questionbank[mCurrentIndex].ismTrueAnswer();
    int messageResId = 0;
    if (userPressedTrue == answerIsTrue) {
        setButtonEnabled(false);
        messageResId = R.string.correct_toast;
    } else {
        setButtonEnabled(false);
        messageResId = R.string.incorrect_toast;
    }
    Toast.makeText(this, messageResId,
            Toast.LENGTH_SHORT)
            .show();
}
public void update()
{
    int ques = questionbank[mCurrentIndex].getmTextResid();
      tquestion.setText(ques);
      if(questionbank[mCurrentIndex].isQuestionAnswered())
        setButtonEnabled(false);
      else
        setButtonEnabled(true);

}

Try this out and check whether this solves your problem试试这个,看看这是否能解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM