2016-12-13 36 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(); 
    } 
}  

我建立在Android的一個簡單的測驗遊戲,目前有一個主菜單的活動,直接連接你的競猜活動,將讓你的「X的有效途徑問題的數量「。最終我想要三個難度級別的測驗。主屏幕上將有3個按鈕,用於指導用戶進行簡易,中等或硬版測驗。我需要在Android Studio中複製現有活動

由於每個活動將與要求的實際問題完全相同。我想知道是否有一種更有效的方式來複制類,而不必重建用戶界面,然後將代碼複製到單獨的類中。

我附上我的代碼的例子,迄今爲止工作得很好。這場比賽非常簡單,對我來說是一個學習練習,而不是任何事情。

回答

1

這樣的事情,最好的方法是使用一個單一的activity

每個activity都會使應用程序變得更重,這就是爲什麼您通常儘可能創建最小數量的activities的原因。

如果您的網頁幾乎是相同的,你應該考慮增加一個簡單的值,做基於價值不同的東西。

button click,只需添加一個int valueintent打開問題activity(1 =容易,2 =中等,3 =硬,或者任何值你喜歡)與

intent.putExtra("lvl", lvl); 

現在,在activity start,撥打這個號碼檢索從intent值與

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

現在只需撥打一個開關:

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

我覺得這裏真正的答案是問題好老「比偏好繼承」偏好。

例如:「收集」您的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方法爲該類。

然後你看看你目前被迫進入你的活動的其他責任。你「切」出來並把它們放到不同的班級中;從而實現更簡單的「重複使用」。