我有<>和<>我的测验应用程序中的按钮,但如果用户是第一个问题,因为没有先前的问题,应该有toast,其中“这是第一个问题”,并且与<>按钮相同。
这是我的按钮代码(不正确):
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurreentIndex=0;
if (mCurreentIndex>mQuestionBank.length){
Toast.makeText(QuizActivity.this,R.string.last_question,Toast.LENGTH_LONG).show();
}
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurreentIndex--;
if (mCurreentIndex<mQuestionBank.length){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
}
updateQuestion();
}
});
答案 0 :(得分:0)
//上一个按钮条件;
mCurreentIndex--;
if (mCurreentIndex<mQuestionBank.length && mCurreentIndex>=0){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(QuizActivity.this, "This is first question",Toast.LENGTH_LONG).show();
mCurreentIndex++;
}
//下一个按钮条件;
mCurreentIndex++;
if (mCurreentIndex<mQuestionBank.length){
Toast.makeText(QuizActivity.this,R.string.last_question,Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(QuizActivity.this, "This is last Question",Toast.LENGTH_LONG).show();
mCurreentIndex--;
}
答案 1 :(得分:0)
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//mCurreentIndex=0;// not valid
if (mCurreentIndex>=mQuestionBank.length){
Toast.makeText(QuizActivity.this,getString(R.string.last_question),Toast.LENGTH_LONG).show();
return;
}
mCurreentIndex++;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurreentIndex<=0){
Toast.makeText(QuizActivity.this, getString(R.string.first_question),Toast.LENGTH_LONG).show();
return;
}
mCurreentIndex--;
updateQuestion();
}
});
答案 2 :(得分:0)
试试这个
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurreentIndex<=0){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
}else{
mCurreentIndex--;
updateQuestion();
}
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurreentIndex>=mQuestionBank.length){
Toast.makeText(QuizActivity.this,getString(R.string.last_question),Toast.LENGTH_LONG).show();
}else{
mCurreentIndex++;
updateQuestion();
}
}
});
答案 3 :(得分:0)
您刚刚显示toast消息,但下一个语句也会在显示toast后执行。对于您的解决方案,您需要使用“返回”或 if..else 阻止。
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurreentIndex=0;
if (mCurreentIndex>mQuestionBank.length){
Toast.makeText(QuizActivity.this,R.string.last_question,Toast.LENGTH_LONG).show();
return;
}
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurreentIndex--;
if (mCurreentIndex<mQuestionBank.length){
Toast.makeText(QuizActivity.this, R.string.first_question,Toast.LENGTH_LONG).show();
return;
}
updateQuestion();
}
});
答案 4 :(得分:0)
公共类QuizActivity扩展了AppCompatActivity {
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPreviousButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[]{
new Question(R.string.question_australia, true),
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas, true),
new Question(R.string.question_asia, true),
};
private int mCurreentIndex = 0;
private boolean[] QuestionsAnswered = new boolean[mQuestionBank.length];
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
if (savedInstanceState != null) {
mCurreentIndex =savedInstanceState.getInt(KEY_INDEX, 0);
QuestionsAnswered = savedInstanceState.getBooleanArray(KEY_INDEX);
}
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurreentIndex = (mCurreentIndex+mQuestionBank.length +1) % mQuestionBank.length;
updateQuestion();
}
});
mPreviousButton = (Button)findViewById(R.id.previous_button);
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurreentIndex= (mCurreentIndex+ mQuestionBank.length - 1) % mQuestionBank.length;
updateQuestion();
}
});
updateQuestion(); }
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(KEY_INDEX,mCurreentIndex);
savedInstanceState.putBooleanArray(KEY_INDEX,QuestionsAnswered);
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
private void updateQuestion(){
int question = mQuestionBank[mCurreentIndex].getTextResId();
mQuestionTextView.setText(question);
mTrueButton.setEnabled(!QuestionsAnswered[mCurreentIndex]);
mFalseButton.setEnabled(!QuestionsAnswered[mCurreentIndex]);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurreentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_LONG).show();
QuestionsAnswered[mCurreentIndex] = true;
mTrueButton.setEnabled(false);
mFalseButton.setEnabled(false);
}
}