简体   繁体   English

多次单击一个按钮以运行相同的功能,java android studio

[英]click a button multiple times to run the same function, java android studio

when i click a button i want to print a random string, but so far i can only click it once then i have to restart to print a new one. 当我单击一个按钮时,我想打印一个随机字符串,但是到目前为止,我只能单击一次,然后必须重新启动才能打印一个新字符串。 what do i have to do to continuously print strings. 我要怎么做才能连续打印字符串。

java code: Java代码:

    public String converse = randomStarter();

public static 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;

}


public void printStarter(View view) {
    ((TextView)findViewById(R.id.fullscreen_content)).setText(converse);

}

xml code to where the text is printed: 将文本打印到的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" />

button xml: 按钮xml:

 <Button android:id="@+id/dummy_button"
            style="?metaButtonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/dummy_button"
            android:clickable="true"
            android:onClick="printStarter"
            />

You're getting the same string because your variable converse always has the same string, when you click the button it doesn't change 您得到相同的字符串,因为您的变量converse始终具有相同的字符串,当您单击按钮时它不会改变

public String converse = randomStarter();

The easy way out would be modifying the code for you button like this: 最简单的方法是修改您的按钮代码,如下所示:

((TextView)findViewById(R.id.fullscreen_content)).setText(randomStarter());

This will work, but your code is very inefficient. 这可以工作,但是您的代码效率很低。 The best way to do it is create an array of strings where you keep all the values, and each time the button is pressed you generate a random number and pull the string from the array whose index corresponds to that number. 最好的方法是创建一个字符串数组,保留所有值,并且每次按下按钮时,都会生成一个随机数,并从索引与该数字对应的数组中拉出字符串。

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

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