2

我想使用文本字段(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);
}

如何在此处添加循环以在按钮单击事件中从字符串数组中随机查找字符串值?

4

1 回答 1

10

您可以使用Random

Random random = new Random(); // or create a static random field...
String randString = questionsOpt[random.nextInt(questionsOpt.length)];
于 2013-04-08T09:39:54.470 回答