import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
public int currentQuestion;
public String[] questions;
public String[] answers;
int score = 0;
Button answerButton;
Button questionButton;
Button homeButton;///NEW ****
public TextView questionView;
public TextView answerView;
public EditText answerText;
public TextView scoreText;
public void main() {
questions = new String[]{"Type Yes", "Type No", "Type And", "Type The"}; /*Array of Hard Coded Questions*/
answers = new String[]{"Yes", "No", "And", "The",}; /*Array of Hard Coded Answers to indexed to match the questions*/
currentQuestion = -1; /*This will index the questions to be used*/
answerButton = (Button) findViewById(R.id.AnswerButton);
questionButton = (Button) findViewById(R.id.QuestionButton);
homeButton = (Button) findViewById(R.id.HomeButton);
questionView = (TextView) findViewById(R.id.QuestionTextView);
answerView = (TextView) findViewById(R.id.AnswerTextView);
answerText = (EditText) findViewById(R.id.AnswerText);
scoreText = (TextView) findViewById(R.id.ScoreText);
///Check the user inserted answer string against the correct or incorrect answers.... NEEDS VALIDATION....
answerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer();
}
});
///Skips to the next questions
questionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getNextQuestion();
}
});
/// Returns you to the Home screen
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, _MainMenu.class));
}
});
}
public void getNextQuestion() {
//1st question so reset everything
if(currentQuestion == -1)
{
setupQuiz();
}
currentQuestion++;
//Check to see if the end of the questions has been reached
if(currentQuestion == questions.length)
{
endQuiz();
}
else
{
questionView.setText(questions[currentQuestion]);
answerText.setText("");
}
}
public void setupQuiz()
{
score = 0;
answerButton.setVisibility(View.VISIBLE);
questionView.setVisibility(View.VISIBLE);
answerText.setVisibility(View.VISIBLE);
answerView.setText("");
questionButton.setText("Skip Question");
}
public void endQuiz()
{
currentQuestion = -1;
answerButton.setVisibility(View.INVISIBLE);
questionView.setVisibility(View.INVISIBLE);
answerText.setVisibility(View.INVISIBLE);
questionButton.setText("Try Again");
scoreText.setText("Final Score: " + score);
}
public void checkAnswer() ///validaion goes here and not in getnextquestion
{
String answer = answerText.getText().toString();
boolean result = answer.equalsIgnoreCase(answers[currentQuestion]);
//answer is correct
if(result == true) {
score += 10; /* ++ will increment the score by 1, +=10 will increment the score by the value added (10)*/
answerView.setText("Correct!");
}/*answerView, text view set to print the string in the event of the correct answer*/
else //answer was wrong
answerView.setText("Incorrect, The answer was " + answers[currentQuestion]); /*answers[currentQuestion] answers reads the answer to the current question in use */
scoreText.setText("Current Score = "+score);
getNextQuestion();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main();
getNextQuestion();
}
}
私は現在、あなたに「x質問の量 "となる。最終的に私はクイズの難易度を3段階にしたい。ホーム画面には3つのボタンがあり、ユーザーはクイズの簡単、中、またはハードバージョンに誘導されます。私はAndroid Studioで既存のアクティビティを効率的に複製する必要があります
それぞれのアクティビティは、実際に質問されている以外のものと全く同じです。私は、ユーザーインターフェイスを再構築せずにコードを別のクラスにコピーしなくても、クラスを数回複製するより効率的な方法があるのだろうかと思っていました。
私はこれまで完全に正常に動作する私のコードの例を添付しました。ゲームはとてもシンプルで、何よりも私のための学習の練習です。
よりも多くの活動の作成を避けるには、イムは今、これに取り組んで、あなたに感謝します。優れた解決策、一般的には将来のための良い練習のように見えます。 –