防止用户输入多个答案

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

preventing a user from entering multiple answer

问题

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);
}
英文:

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.

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&lt;=s  )
{
if (mCurrentIndex + 1 &lt; questionbank.length) {
mCurrentIndex++;
}
else
{   Toast.makeText(getApplicationContext(),&quot;not more question&quot;,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 &gt; 0) {
mCurrentIndex = (mCurrentIndex - 1);
update();
}
else
{ Toast.makeText(getApplicationContext(), &quot;cant go back&quot;, 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);
}

}

答案1

得分: 2

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

public class Questions {

    boolean isQuestionAnswered;

    public boolean isQuestionAnswered() {
        return isQuestionAnswered;
    }

    public void setQuestionAnswered(boolean questionAnswered) {
        isQuestionAnswered = questionAnswered;
    }
}

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

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();
}

公共方法update,

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

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

英文:

In Questions class you can create a method,

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

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:

确定