本文介绍了创建一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个按钮,这将启动randomGenerator。
I am trying to create a button which would start the randomGenerator.
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];
下面是我的布局,我已经创建了我的按键和标记它。只是不知道如何从这里着手。
Here's my layout, I have already created my button and labeled it. Just not sure how to proceed from here.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".ImHungry" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/button1" />
</RelativeLayout>
三江源!
推荐答案
您可以做这样的事情:
添加安卓的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];
}
希望有所帮助。
这篇关于创建一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!