SoFunction
Updated on 2025-03-04

Android APP writing a simple answerer

This article shares with you a simple answerer written by Android APP. This answerer can select the next question through the Next button. The new question class question has two member variables.

Java code:

package ; 
 
/** 
 * Created by wang on 16-10-19. 
 */ 
public class Question { 
  private int mTextResId; 
  private boolean mAnswerTrue; 
 
  public Question(int textResId, boolean answerTrue) { 
    mTextResId = textResId; 
    mAnswerTrue = answerTrue; 
 
  } 
 
  public int getTextResId() { 
    return mTextResId; 
  } 
 
  public boolean isAnswerTrue() { 
    return mAnswerTrue; 
  } 
 
  public void setTextResId(int textResId) { 
    mTextResId = textResId; 
  } 
 
  public void setAnswerTrue(boolean answerTrue) { 
    mAnswerTrue = answerTrue; 
  } 
} 

Java code:

package ; 
 
import .; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class QuizActivity extends AppCompatActivity { 
  private Button mTrueButton; 
  private Button mFalseButton; 
  private Button mNextButton; 
  private TextView mQuestionTextView; 
  private Question[] mQuestionBank = new Question[] { 
      new Question(.question_oceans, true), 
      new Question(.question_mideast, false), 
      new Question(.question_africa, false), 
      new Question(.question_americas,true), 
      new Question(.question_asia, true), 
  }; 
  private int mCurrentIndex = 0; 
 
  private void updateQuestion() { 
    int question = mQuestionBank[mCurrentIndex].getTextResId(); 
    (question); 
  } 
 
  private void checkAnswer(boolean userProessedTrue) { 
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue(); 
    int messageId = 0; 
    if (userProessedTrue == answerIsTrue) 
      messageId = .correct_toast; 
    else 
      messageId = .incorrect_toast; 
    (this, messageId, Toast.LENGTH_SHORT).show(); 
  } 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    (savedInstanceState); 
    setContentView(.activity_quiz); 
 
    mQuestionTextView = (TextView) findViewById(.question_test_view); 
    // int question = mQuestionBank[mCurrentIndex].getTextResId(); 
    // (question); 
    updateQuestion(); 
 
    mTrueButton = (Button) findViewById(.true_button); 
    (new () { 
      @Override 
      public void onClick(View v) { 
        // Does nothing yet, but soon! 
       /* (, 
            .incorrect_toast, 
            Toast.LENGTH_SHORT).show(); */ 
        checkAnswer(true); 
      } 
    }); 
    mFalseButton = (Button) findViewById(.false_button); 
    (new () { 
      @Override 
      public void onClick(View v) { 
        // Does nothing yet, but soon! 
       /*  (, 
            .correct_toast, 
            Toast.LENGTH_SHORT).show(); */ 
        checkAnswer(false); 
      } 
    }); 
    mNextButton = (Button) findViewById(.next_button); 
    (new () { 
      @Override 
      public void onClick(View v) { 
        mCurrentIndex = (mCurrentIndex + 1) % ; 
        // int question = mQuestionBank[mCurrentIndex].getTextResId(); 
        // (question); 
        updateQuestion(); 
      } 
    }); 
  } 
} 

xml code:

<LinearLayout xmlns:andro 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:gravity="center" 
  android:orientation="vertical" > 
  <TextView 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="24dp"/> 
  <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <Button 
      android: 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button"/> 
    <Button 
      android: 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/false_button"/> 
  </LinearLayout> 
  <Button 
    android: 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/next_button"/> 
  </LinearLayout> 

Code:

<resources> 
  <string name="app_name">GeoQuiz</string> 
  <string name="question_text"> 
    Constantinople is the largest city in Turkey. 
  </string> 
  <string name="true_button">True</string> 
  <string name="false_button">False</string> 
  <string name="correct_toast">Correct!</string> 
  <string name="incorrect_toast">Incorrect!</string> 
  <string name="action_settings">Settings</string> 
  <string name="next_button">Next</string> 
  <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string> 
  <string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string> 
  <string name="question_africa">The source of the Nile River is in Egypt.</string> 
  <string name="question_americas">The Amazon River is the longest river in the Americas.</string> 
  <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string> 
</resources> 


The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.