Я создал свою первую математическую игру, все работает нормально, но makeText не работает. Когда я нажимаю на ответ, ничего не происходит, не получается "Well Done!"
или "Sorry that's wrong"
.makeText не работал над моей первой игрой в студии Android
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class GameActivity extends Activity implements View.OnClickListener {
int correctAnswer;
Button buttonObjectChoice1;
Button buttonObjectChoice2;
Button buttonObjectChoice3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
//ovde inicijalizujem sve varijable
int partA = 9;
int partB = 9;
int correctAnswer = partA * partB;
int wrongAnswer1 = correctAnswer + 1;
int wrongAnswer2 = correctAnswer - 1;
/*ovde povezujem objekte sa odgovarajucim UI-em*/
TextView textObjectPartA = (TextView) findViewById(R.id.textPartA);
TextView textObjectPartB = (TextView) findViewById(R.id.textPartB);
Button buttonObjectChoice1 =(Button) findViewById(R.id.buttonChoice1);
Button buttonObjectChoice2 =(Button) findViewById(R.id.buttonChoice2);
Button buttonObjectChoice3 =(Button) findViewById(R.id.buttonChoice3);
/*sada cu koristiti setText metodu za moje nove objekte
da pokazem vrednost na Ui dizajnu.
Just like when we output to the console in the exercise -
Expressions in Java, only now we use setText method
to put the values in our variables onto the actual UI. */
textObjectPartA.setText("" + partA);
textObjectPartB.setText("" + partB);
//which button receives which answer, at this stage is arbitrary.
buttonObjectChoice1.setText("" + correctAnswer);
buttonObjectChoice2.setText("" + wrongAnswer1);
buttonObjectChoice3.setText("" + wrongAnswer2);
}
@Override
public void onClick(View v) {
//declare a new int to be used in all the cases
int answerGiven=0;
switch (v.getId()) {
case R.id.buttonChoice1:
//initialize a new int with the value contained in buttonObjectChoice1
//Remember we put it there ourselves previously
answerGiven = Integer.parseInt("" + buttonObjectChoice1.getText());
//is it the right answer?
if(answerGiven==correctAnswer)
{//yay it's the right answer
Toast.makeText(getApplicationContext(), "Well done!",
Toast.LENGTH_LONG).show();
}
else{
//uh oh!
Toast.makeText(getApplicationContext(),"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice2:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice2.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
case R.id.buttonChoice3:
//same as previous case but using the next button
answerGiven = Integer.parseInt("" + buttonObjectChoice3.getText());
if(answerGiven==correctAnswer) {
Toast.makeText(getApplicationContext(), "Well done!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(),"Sorry that's wrong", Toast.LENGTH_LONG).show();
}
break;
}
}
}
Добро пожаловать в Stack Overflow Bodin! Пожалуйста, попробуйте ввести ясный английский (т. Е. Не l33t говорить), чтобы максимально увеличить вероятность ответа на ваш вопрос. Вы пытались перенаправить проблему на минимальный объем кода, чтобы воспроизвести проблему? – OldTinfoil
Благодарим за редактирование sugest. Im rookie здесь и в программировании, поэтому я не знаю, как перегонять проблему до минимального количества кода. – Bodin