этот код работает очень хорошо, он генерирует три кнопки, потому что у меня есть три элемента «car» и элементы случайного выбора: button_1 «a», button_2 «r», button_3 «c», моя проблема в том, когда я нажимаю на каждая кнопка, которую он набирает в тексте редактирования, стала "aaa"
//the problem was here (var button)
edittext.setText(edittext.getText().toString() + button.getText());
я пытаюсь заставить игру угадывать слово
String array[]= {"car"};
int random = (int) (Math.random() * array.clone().length);
StringBuilder word = new StringBuilder(array[random]);
for (int i = 0; i < array[random].length(); i++) {
char id = 'b';
final Button button = new Button(this); //local var
button.setId(id + i);
linearLayout.addView(button);
int randIndex = new Random().nextInt(word.length());
button.setText("" + word.charAt(randIndex));
word.deleteCharAt(randIndex);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//******the problem was here (var button)
button.setVisibility(View.INVISIBLE);
edittext.setText(edittext.getText().toString() + button.getText());
}
});
}