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

标签 android arrays loops android-arrayadapter

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

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

最佳答案

您可以使用 Random :

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

关于android - 如何从字符串数组中随机获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875847/

相关文章:

Android:在 Android Studio 中调试源代码

android - 证书异常 - OpenSSLX509CertificateFactory$ParsingException

java - 从二维数组中删除元素

ios - 没有从 [UInt] 数组到数据的转换

javascript - while 循环和 "this"对象

javascript - 用户代理,如何自动为不同的设备设置不同的 css 文件

android - 为 Assets 和资源文件夹指定其他路径-Android Studio

python - 我可以在 Python 列表上创建 "view"吗?

python - 'for' 循环中 i = i + 1 和 i += 1 有什么区别?

c - 如何保持C程序运行