2012-08-26 98 views
0

我是一名初学者,我正在制作一个Android应用程序。我的目标是当按钮被按下时,应用程序会随机返回单词列表中的特定单词。我应该如何去做这件事?另外,我使用的是eclipse,根本没有太多经验。从列表中显示随机单词

+0

什么样的名单在您的TextView显示wordToDisplay?你能显示一些代码吗? – Tudor

+0

只是一个名称为 – user1545666

回答

3

既然您已经在您的问题中提到您要从单词列表中生成一个单词,那么我认为您已经准备好了列表。

使用以下命令生成一个随机数,然后显示对应于该索引的单词。

Random randomGenerator = new Random(); 
int randomInt = randomGenerator.nextInt(100); // maximum value 100; you can give this as the maximum index in your list of words 

:不使用的Math.random(它产生的双打,而不是整数)

import java.util.Random;

编辑:

假设您有包含30个字符的字符串数组中的单词列表DS。

String wordList[] = new String[30]; 
// enter the words in the wordList (if you have a String, use a Tokenizer to get the individual words and store them in wordList) 
int randomInt = randomGenerator.nextInt(30); 
String wordToDisplay = wordList[randomInt]; 

然后,您可以通过使用mTextView.setText(wordToDisplay);

+0

的列表,它只是产生一个随机的int。他在问题中提出了其他要求。 – Tudor

+1

是的,所以他可以使用随机数的索引并显示相应的单词。 – Swayam

+0

然后请用此更新您的答案。 – Tudor