如何从字符串数组中随机获取文本?

Eng*_*ain 2 arrays android loops android-arrayadapter

我想从click事件的字符串数组中随机获取文本值,使用文本字段(AutoCompleteTextView).

java文件在这里:

String[] questionsOpt = { "I just ejaculated blood", "I just eat", "I just emptied my 401k", "I just exist", 
        "tattooed my face", "threw up yellow stuff", "threw up in my mouth", "took a huge dump"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start_game);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, questionsOpt);

    AutoCompleteTextView actvDev = (AutoCompleteTextView) findViewById(R.id.actvDev);
    actvDev.setThreshold(1);
    actvDev.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)

如何在这里添加循环,以便在按钮点击事件中从字符串数组中随机查找字符串值?

MBy*_*ByD 10

你可以使用Random:

Random random = new Random(); // or create a static random field...
String randString = questionsOpt[random.nextInt(questionsOpt.length)];
Run Code Online (Sandbox Code Playgroud)