2012-06-01 57 views
0

我是新來的Android應用程序的開發,我試圖找到一種方法,通過一系列採取用戶輸入屏幕進行。我正在做一個小型的數學遊戲,用戶可以回答基本的兩個整數加法問題。到目前爲止主菜單已創建。它有一個新遊戲按鈕,它啓動了我的GameActivity,它對一個數學問題運行得很好。然而,用戶輸入自己的答案簡單的數學問題後,我希望能夠繼續到另一個數學題,一旦用戶已經回答當前的問題和方法將返回正確/不正確的值。起初,我以爲從GameActivity內做這樣的事情FOR循環基本的:另一個Android活動或迭代JAVA方法?

for(int i = 0; i < 10; i++){ gameMethod(); } 

gameMethod()是一個簡單的生成2張隨機數,將其添加到得到正確的答案的Java方法,然後使用提示用戶一個EditText字段來輸入他們的猜測。它顯示隨機數並使用XML佈局文件中創建的TextView框進行回答。

每次調用gameMethod會,至少我覺得,只是重新輸入隨機數到屏幕的每個迭代上顯示的TextView的領域。我真的不在乎以前的數據是什麼,所以我想我會覆蓋他們。儘管如此,我無法獲得代碼。我放了一個Log.d()語句或兩個,發現FOR循環實際上正確運行了10次,但它沒有等待用戶輸入,然後纔開始下一個gameMethod()。

在做一些環視時,我找到了startActivityForResult(),並想知道這是否是一種更恰當的方法來解決這個問題。所以通過這種方式,我預見到有三個活動,調用GameActivity的Main菜單,然後迭代10次,每次迭代調用另一個活動GameScreenActivity,它將實際將數字放在屏幕上,讀入用戶輸入並讀入然後返回1表示正確答案,0表示錯誤答案。到目前爲止,在閱讀StarActivityForResult()時,我對這個過程感到有點困惑,並想知道這是否是一個可能的探索路徑。

同樣,我在這個Android編程很新和欣賞任何和所有幫助,我可以得到的。

謝謝。

對不起,最初沒有包含gameMethod(),我在下面添加了它。

// Create and initialize arrays of integers 
    int[] a = {0,1,2,3,4,5,6,7,8,9,10}; 
    int[] b = {0,1,2,3,4,5,6,7,8,9,10}; 

    // Creates random number generator 
    Random randy = new Random(); 

    // Generates two random values to add 
    int r1 = randy.nextInt(10); 
    int r2 = randy.nextInt(10); 

    // Calculates correct answer 
    int an = a[r1] + a[r2]; 

    // Displays 1st number 
    TextView number1 = (TextView) findViewById(R.id.firstNumber); 
    number1.setText(Integer.toString(a[r1])); 

    // Displays 2nd number 
    TextView number2 = (TextView) findViewById(R.id.secondNumber); 
    number2.setText(Integer.toString(b[r2])); 

    // Displays correct answer 
    TextView answer1 = (TextView) findViewById(R.id.answerNumber); 

    //hide answer until user puts in theirs 
    answer1.setVisibility(View.INVISIBLE); 
    answer1.setText(Integer.toString(an)); 

    //hide the answer place holder value 
    TextView uAnswerDisplay = (TextView) findViewById(R.id.userAnswerNumber); 
    uAnswerDisplay.setVisibility(View.INVISIBLE); 

    //Get the EditText field that the user will input their answer to 
    EditText inputText = (EditText) findViewById(R.id.userInput); 

    //set up a listener to run once the ENTER key is hit after their answer has been entered 
    inputText.setOnKeyListener(new EditText.OnKeyListener(){ 
     public boolean onKey(View v, int keyCode, KeyEvent event){ 

    //only go on the ENTER key when pressed DOWN 
    if((event.getAction() == KeyEvent.ACTION_DOWN) && 
      (keyCode == KeyEvent.KEYCODE_ENTER)){ 

    EditText innerInputText = (EditText) findViewById(R.id.userInput); //get the EditText box reference 
       TextView innerUAnswerDisplay = (TextView) findViewById(R.id.userAnswerNumber); //get the TextView box that the answer will go in 
       String inputString = innerInputText.getText().toString(); //capture the user's input 

       int uAnswer = Integer.parseInt(inputString); //parse user answer to an integer 
       int cAnswer = Integer.parseInt((((TextView) findViewById(R.id.answerNumber)).getText()).toString()); 
       innerUAnswerDisplay.setText(Integer.toString(uAnswer)); //display the string after converting from integer 

       //change colors of text based on correctness 
       if (uAnswer == cAnswer){ innerUAnswerDisplay.setTextColor(Color.GREEN); } //green for correct 
       else { innerUAnswerDisplay.setTextColor(Color.RED); } //red for incorrect 

       innerUAnswerDisplay.setVisibility(View.VISIBLE); //make the user input answer visible 

       TextView innerAnswer1 = (TextView) findViewById(R.id.answerNumber); 
       innerAnswer1.setVisibility(View.VISIBLE); //hide answer until user puts in theirs 
      } //end of if 
      return false; //return false 
     } //end of onKey 

    }); //end of setOnKeyListener 

對不起,所有的編輯,我無法得到的編輯,包括代碼並正確張貼,所以我把它分成塊,並加入少許的時間。

+0

你說「gameMethod」提示用戶輸入答案。如果您向我們提供提示用戶的代碼,這將有所幫助。一個「提示」通常只會在您啓動一個活動,片段,對話框或對話框片段或類似的東西時纔會發生。 – Christine

回答

0

從你說的話,我會考慮讓用戶回答問題兩種方式:

  1. 對觸發一個新的循環輸入的EditText一個onclick偵聽器;
  2. 有一個對話框活動,從循環開始開始,提示用戶輸入新答案,遊戲活動將通過onActivityResult接收答案。
相關問題