У меня есть около 150 различных текстов, которые я хочу показать в случайном порядке, когда я нажимаю Button
. Новый каждый раз, когда я нажимаю Button
. Я понял, этот код:Случайный текст при нажатии кнопки
Random myRandom = new Random();
TextView textblondin = (TextView) findViewById(R.id.textblondin);
switch(myRandom.nextInt() %3) {
case 0:
textblondin.setText("Text 1");
break;
case 1:
textblondin.setText("Text 2");
break;
case 2:
textblondin.setText("Text 3");
break;
default:
break;
}
}
}
я могу получить его связан с Button
. Кто-нибудь знает, как это сделать?
public class Blondinskamt extends Activity { <----X
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.blondintext);
View myRandom = findViewById(R.id.myRandom);
myRandom.setOnClickListener(null);
myRandom.setOnClickListener(new View.OnClickListener() {
Random myRandom = new Random();
TextView textblondin = (TextView) findViewById(R.id.textblondin); <----X
switch(myRandom.nextInt() %3) {
case 0:
textblondin.setText("Skämt");
break;
case 1:
textblondin.setText("Roligt");
break;
case 2:
textblondin.setText("kul kul kul kul");
break;
default:
}}
Я все еще получаю ошибки, где я поставил "< ---- X", что я делаю неправильно?
use Button myRandom = findViewById (R.id.myRandom); myRandom.setOnClickListener (новый View.OnClickListener() { public void onClick (View v) { // Выполнение действия при нажатии } }); –