I would like to have a button and when you click the button it displays a phrase at random. I've done a little research and (I have a limited knowledge of java) i believe i would have to use an array and it display a random string of text. can someone tell me what code i开发者_运维百科'd have to use?
The Android API example TextToSpeechActivity does exactly what you want.
Here's the relevant snippet of code:
private static final Random RANDOM = new Random();
private static final String[] HELLOS = {
"Hello",
"Salutations",
"Greetings",
"Howdy",
"What's crack-a-lackin?",
"That explains the stench!"
};
private void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[RANDOM.nextInt(helloLength)];
}
精彩评论