2016-12-13 5 views
0
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で既存のアクティビティを効率的に複製する必要があります

それぞれのアクティビティは、実際に質問されている以外のものと全く同じです。私は、ユーザーインターフェイスを再構築せずにコードを別のクラスにコピーしなくても、クラスを数回複製するより効率的な方法があるのだろうかと思っていました。

私はこれまで完全に正常に動作する私のコードの例を添付しました。ゲームはとてもシンプルで、何よりも私のための学習の練習です。

答えて

1

このような場合の最良の方法は、単一のactivityを使用することです。

すべてactivityは、アプリケーションを非常に重くするため、通常は可能な限り最小の数のactivitiesを作成しようとします。

ページがほぼ同じ場合は、単純な値を追加し、その値に基づいてさまざまな処理を行うことを検討する必要があります。button click

、単にactivity start上の質問activity(1 =容易で、2 =中、3 =ハード、またはあなたが好きな任意の値)

intent.putExtra("lvl", lvl); 

とし、開くintentint valueを追加し、

int myLvl = getIntent().getIntExtra("lvl", 0); 

intentから値を取得し、この番号に電話して、今、単純にスイッチを呼び出す:

switch(myLvl){ 
    case 1: doLvl1Stuffs(); 
      break; 
    case 2: doLvl2Stuffs(); 
      break; 
    case 3: doLvl3Stuffs(); 
      break; 
    default: throw new Exception("no lvl found"); 
      break; 

・ホープ、このことができますが、一般的に必要なもの

+0

よりも多くの活動の作成を避けるには、イムは今、これに取り組んで、あなたに感謝します。優れた解決策、一般的には将来のための良い練習のように見えます。 –

0

最良の方法は、相互のコンポーネントと他の活動は、それから延び、そのロジック

を実装するすべて含まれていBaseActivityを作成することですが、あなたが言うように、それはすべて同じです。違いは質問ですので、あなたは同じ活動を使用することができ、現在の難易度を知るためのフラグを設定し、それに基づいて質問を設定する複数の活動を作成する必要はありません

1

私は本当の答えはここにあります良い古い "相続よりも組成を好む" mem。

例:あなたのUI要素を「収集」します。あなたは

public class QuizButtonManager { 
    private final Button answerButton; 
    private final Button questionButton; 
    private final Button homeButton; 

    public QuizButtonManager(Button answerButton ...) { 
    this.answerButton = answerButton; 
    ... 

そして、あなたは、単に動き、そのクラスにあなたsetupQuiz()endQuiz方法のようなヘルパークラスを作成することができます。

あなたは、あなたが現在あなたの活動に強制してきた他の責任を調べます。あなたはそれらを「切り取って」別々のクラスに入れます。はるかに簡単な「再利用」が可能になります。

関連する問題