且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

创建一个按钮

更新时间:2023-11-17 15:30:22

您可以做这样的事情:

添加安卓的onClick =randomGenerator你的按钮属性。然后在你的活动。
使一个被点击的按钮时要调用的函数。 (我假设你想要做你的随机生成code点击按钮时)。

Add android:onClick="randomGenerator" to your button attribute. Then in your activity. make a function that will be called when your button is clicked. (I assumed that you wanted to do your random generation code when the button is clicked.)

public void randomGenerator(View view){

    Random randomGenerator = new Random();
    int randomInt = randomGenerator.nextInt(9);
    String wordList[] = new String[9];
    {
        wordList[0] = "Mexican";
        wordList[1] = "American";
        wordList[2] = "Barbeque";
        wordList[3] = "Chinese";
        wordList[4] = "Indian";
        wordList[5] = "Italian";
        wordList[6] = "Thai";
        wordList[7] = "Viatnamese";
        wordList[8] = "Middle Eastern";

    }

    String wordToDisplay = wordList[randomInt];

}

希望有所帮助。