2014-04-05 2 views
0

안드로이드 앱에서 사용자가 임의의 합계에 답변 할 수있게하려는 경우 새 앱이 화면에 표시됩니다. 이것은 10 번 반복되고 마지막 점수가 주어진다. 그러나 나는 각각의 새로운 랜덤이 화면에 보여 지도록 합계를 업데이트하는 방법을 확신하지 못합니다. 다음은 Android : 각 클릭 후 새로운 임의의 합계?

내 현재 코드입니다 : 사용자 언론이 질문을 업데이트 버튼을 깨끗하고 이전의 모든 값을 제출할 때 있도록

public class Test extends Activity { 
    //declare vars 
    TextView text; 
    EditText answer; 
    Button submit; 
    int random1; 
    int random2; 
    String question; 
    int correctAnswer;@ 
    Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 
     // initialising variables 
     initialiseVars(); 
     //set up random 
     setUpRandom(); 
     //Set text view equal to question 
     text.setText(question); 
     //updateQuestion? 
    } 
    public void initialiseVars() { 
     text = (TextView) findViewById(R.id.tvTopRandomTest); 
     answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest); 
     submit = (Button) findViewById(R.id.btnSubmitRandomTest); 
    } 
    public void setUpRandom() { 
     //setting up randoms 
     Random random = new Random(); 
     // Generating random number between 1 and 12 
     random1 = random.nextInt(12) + 1; 
     // Generating another random number between 1 and 12 
     random2 = random.nextInt(12) + 1; 
     question = random1 + " x " + random2 + " = "; 
     correctAnswer = random1 * random2; 
    } 
    public void updateQuestion() { 
     //CODE TO UPDATE QUESTION 
    } 
} 
+1

무엇이 문제입니까? –

+0

사용자가 '제출'을 클릭 할 때마다 번호를 변경 하시겠습니까? –

+0

사용자가 합계에 대한 답변을 입력 할 때마다 텍스트 뷰를 응답 할 수 있도록 다른 임의의 합계로 새로 고치기를 원합니다. –

답변

2

이 버튼에 ClickListener 추가

submit = (Button) findViewById(R.id.btnSubmitRandomTest); 
submit.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     updateQuestion(); 
    } 
} 

활동의 수를 유지하고 updateQuestion에서 증가 시키십시오. 질문 모음

public void updateQuestion() { 
    if (Int.parseString(answer.getText().toString()) != correctAnswer) { 
     // Show toast or something 
     return; 
    } 
    tries++; 
    if (tries == 10) return; // or do something else; 
    answer.setText(""); 
    setUpRandom(); 
    text.setText(question); // add this line in your setUpRandom(); 
} 

티저는 this을 봅니다. 바라기를 이것은 당신을 도울 것입니다.

+1

그 방법입니다!^-^ – Lasse

+0

고맙습니다. 사용자가 대답 한 질문과 질문 활동에서 제공 한 답변을 보여주고 싶다면 두 가지 배열을 모두 통과해야합니다. 질문 및 그 대답의 배열을 의도를 통해 다음 활동으로 안내합니까? –

+0

질문 및 답변 목록을 유지해야합니다 (또는 질문과 답변을 모두 포함하는 클래스를 만들고 Parcelable 클래스에서 구현해야 함). –

관련 문제