数学ゲームを作成していますが、ボタン クリック メソッドで正解/不正解を表示するのに問題があります。基本的に、方程式メソッド内に if/else ステートメントを作成しました。その中で、ステートメントは、ユーザー入力が正しい答えに一致する場合、テキストビューのテキストを「正しい」、そうでない場合は「間違っている」に変更するなどのことを行うと想定されています。これは、ユーザーがボタンをクリックしたときに発生すると想定されています。
何らかの理由で何もしない私のコードは次のとおりです。
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.gamelayout);
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_EASY);
edittext = (EditText) findViewById(R.id.USERentry);
questionLabel = (TextView) findViewById(R.id.question_label);
answerLabel = (TextView) findViewById(R.id.rightwrong_label);
button1 = (Button) findViewById(R.id.keypad_1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks
//String buttonText = (String) button.getText();
edittext.setText(edittext.getText() + "1");
//edittext.setText() = edittext.getText() + "1";
}
});
button2 = (Button) findViewById(R.id.keypad_2);
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
edittext.setText(edittext.getText() + "2");
}
});
...some more buttons...
buttonhash = (Button) findViewById(R.id.keypad_hash);
buttonhash.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
answerLabel.getText();
answerLabel.setBackgroundColor(R.color.background_answer);
}
});
getGame(diff);
}
public void Easy12(){
Random rand = new Random();
final int a = (int) rand.nextInt(20)+1;
final int b = (int) rand.nextInt(20)+1;
String aString = Integer.toString(a);
String bString = Integer.toString(b);
String display = aString + " + " + bString + " =";
questionLabel.setText(display);
final int c = a + b;
if(edittext.getText().toString().equals(String.valueOf(c))){
answerLabel.setText(R.string.answer_correct);
answerLabel.setTextColor(R.color.correct_color);
}
else
{
answerLabel.setText(R.string.answer_wrong);
answerLabel.setTextColor(R.color.wrong_color);
//answerLabel.setBackgroundColor(R.color.background_answer);
}
}
気にしないで、私はそれを働かせました。
基本的に、onCreate() で、buttonhash.setOnClickListener(this); と言って onClickListener を設定する必要がありました。
また、クラス Game の OnClickListener を実装する必要がありました。
そして最後に、onClick(View v) メソッドを作成し、各ボタンの動作をコーディングできます。:) 幸せな日々。