如何从字符串数组中随机获取文本?
问题描述:
我想要使用文本字段(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)];
TNX求助本雅明Sharet – 2013-04-08 10:10:04
发送代码示例breifly,即不能正常工作 – 2013-04-09 09:56:53