这是一个生成随机字符串的类文件。
public class RandomGen {
static String keys() {
String str[] = new String[25];
str[0] = "Press A1";
str[1] = "Press A2";
str[2] = "Press A3";
str[3] = "Press A4";
str[4] = "Press A5";
str[5] = "Press B1";
str[6] = "Press B2";
str[7] = "Press B3";
str[8] = "Press B4";
str[9] = "Press B5";
str[10] = "Press C1";
str[11] = "Press C2";
str[12] = "Press C3";
str[13] = "Press C4";
str[14] = "Press C5";
str[15] = "Press D1";
str[16] = "Press D2";
str[17] = "Press D3";
str[18] = "Press D4";
str[19] = "Press D5";
str[20] = "Press E1";
str[21] = "Press E2";
str[22] = "Press E3";
str[23] = "Press E4";
str[24] = "Press E5";
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(25);
return str[randomInt];
}
}
这是安卓活动文件。我从一个类中返回一个随机文本并在文本视图中显示。如果返回的字符串是“Press A1”,用户应该只按下 Al 按钮。如果他按下其他按钮,则必须在其他文本视图中显示错误消息。下次如果该文本视图中显示“Press C1”,用户应该只按下 C1 按钮。如果他按下其他按钮,则必须显示“错误按钮”消息。我不知道如何检查条件。请帮我。
RandomGen rg = new RandomGen();
String s;
b[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
s = rg.keys();
tv.setText(s);
tv2.setText("A1 is pressed");
}
});
b[1].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
s = rg.keys();
tv.setText(s);
tv2.setText("A2 is pressed");
}
});
b[2].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
s = rg.keys();
tv.setText(s);
tv2.setText("A3 is pressed");
}
});
b[3].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
s = rg.keys();
tv.setText(s);
tv2.setText("A4 is pressed");
}
});