开发者

Display Random Text on button click

开发者 https://www.devze.com 2023-02-05 16:26 出处:网络
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

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)];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消