android - ANDROID STUDIO 中没有重复的随机文本
问题描述
我在 android studio 中有这段代码,我在 TextView 中显示了这些短语,但我不知道如何在所有问题都出现之前不重复这些问题。或显示一些已显示的消息。非常感谢!我需要帮助!
import java.util.Random;
public class Final extends AppCompatActivity {
Button btn_Generate;
TextView edt_Questions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
btn_Generate = findViewById(R.id.btn_Generate1);
edt_Questions = findViewById(R.id.textView11);
final String questions[] = {
"Aquellas personas que crean en fantasmas beben (Sólo existe un único fantasma, y es el alcohol).",
"Imita a un jugador. Quien lo adivine manda un trago/shot.",
"Imita a un famoso, quien lo adivine manda un trago/shot.",
"Señala al jugador mas guapo/a. Ese jugador manda un trago/shot.",
};
btn_Generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random rand = new Random();
int Questions = rand.nextInt(4);
edt_Questions.setText(questions[Questions]);
}
});
}
}
解决方案
Button btn_Generate;
TextView edt_Questions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
btn_Generate = findViewById(R.id.btn_Generate1);
edt_Questions = findViewById(R.id.textView11);
final ArrayList<String> questions = new ArrayList<>();
final ArrayList<String> selected = new ArrayList<>();
questions.add("Aquellas personas que crean en fantasmas beben (Sólo existe un único fantasma, y es el alcohol).");
questions.add("Imita a un jugador. Quien lo adivine manda un trago/shot.");
questions.add("Imita a un famoso, quien lo adivine manda un trago/shot.");
questions.add("Señala al jugador mas guapo/a. Ese jugador manda un trago/shot.");
btn_Generate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (questions.isEmpty()) {
questions.addAll(selected);
selected.clear();
}
Random rand = new Random(System.currentTimeMillis());
int id = rand.nextInt(questions.size());
String question = questions.get(id);
selected.add(question);
questions.remove(question);
edt_Questions.setText(question);
}
});
}
推荐阅读
- knuth - Mastermind 的 Donald Knuth 算法——我们能做得更好吗?
- javascript - 如何使用 JavaScript 中的过滤器根据用户输入过滤整个数组?
- python - 将可选事件传递给具有默认参数的方法
- bash - 单行排除异常模式
- python - 仅当两个值都存在时才加入 Pandas 中的列
- tableau-api - 无法在 tableau 中的图表上显示计算
- php - 使用 php 从 sql 获取日期时间
- angular - 如何在 Angular 材质中使 mat-checkbox 不可点击
- jax-rs - 无法使用 ConfigBean 和 @RunAs 填充 BD
- powershell - Windows 自定义不适用于 terraform