首页 > 解决方案 > 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]);
            }
        });
    }
}

标签: androidandroid-studio

解决方案


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);
        }
    });
}

推荐阅读