简体   繁体   English

android按钮连续生成字符串

[英]android button continuously generate strings

When I press a button it prints one of the strings at random, but I can only do it once then I have to restart the app and press again to print a new one. 当我按下一个按钮时,它会随机打印其中一个字符串,但是我只能执行一次,然后必须重新启动该应用程序,然后再次按下以打印一个新字符串。 What do I have to do to be able to continuously press the button and continuously print the strings at random? 我要怎么做才能连续按下按钮并连续不断地随机打印字符串?

public String converse = randomStarter();

public String randomStarter() {
    Random generator = new Random();
    int rand = generator.nextInt(6);
    String starter = new String("");

    switch (rand) {
        case 0: starter = "What was your favorite subject in \n school as a kid? \n Worst?"; 
            break;

        case 1: starter = "My favorite room in the house is...";
            break;

        case 2: starter = "If you had 1 million dollars, what would you do with it?";
            break;

        case 3: starter = "Did you ever have a nickname? \n If so, what was it?";
            break;

        case 4: starter = "If I had magical powers I would...";
            break;

        case 5: starter = "If you were invisible for a day, \nwhat would you do?";
            break;

        case 6: starter = "5 people I would NOT like to meet...";
            break;
    }

    return starter;
}

xml code for text field: 文本字段的xml代码:

   <TextView android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    android:textColor="#2f4b66"
    android:textStyle="bold"
    android:textSize="50sp"
    android:gravity="center"
    android:text="@string/dummy_content" />

Your code's hinting that you use converse as your 'random' string. 您的代码暗示您将converse用作“随机”字符串。 If you want to generate new random stings, you'll have to call randomStarter() every time. 如果要生成新的随机randomStarter()每次都必须调用randomStarter()

If this is not the case, please show the code where you're using the random string. 如果不是这种情况,请在使用随机字符串的地方显示代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM