1

我正在尝试创建一个按钮来启动 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];

这是我的布局,我已经创建了我的按钮并标记了它。只是不知道如何从这里开始。

<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>

谢谢你!

4

4 回答 4

0

在 onCreate 方法中尝试这样的事情:

Button btnStart = (Button) findViewById(R.id.button1);
TextView tv = (TextView) findViewById(R.id.textview1);

btnStart.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(final View v) {
        tv.setText(wordToDisplay);
        }
    });

并且不要忘记在 TextView 中设置一个 id。IE

<TextView
    android:id="textview1"
...
于 2012-08-26T20:14:12.780 回答
0
Button mButton; 
TextView mTextView;
String wordList[] = new String[9];


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main); 

mButton = (Button)findViewById(R.id.button1); 
mTextView = (TextView) findViewById(R.id.textview1);
mButton.setOnClickListener(onClick); 


    {
        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";

    }

    }



    private View.OnClickListener onClick= new View.OnClickListener() {
            // @Override
            public void onClick(View v) {

            Random randomGenerator = new Random();
         int randomInt = randomGenerator.nextInt(9);
         String wordToDisplay = wordList[randomInt];
        mTextView.setText(wordToDisplay);
        }

    };

注意:在您的代码中,您的 TextView 缺少 id 属性。确保添加它。

<TextView
    android:id="textview1"
...
于 2012-08-26T20:10:49.037 回答
0

如果我说对了,您应该将其添加到您的onCreate()

     final Button button1 = (Button) findViewById(R.id.button1);
     button1.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
             Random randomGenerator = new Random();
             int randomInt = randomGenerator.nextInt(9);
             String wordToDisplay = wordList[randomInt];

             // display in TextView
             TextView tv = (TextView) findViewById(R.id.textview1);
             tv.setText(wordToDisplay);
         }
     });

并添加android:id="textview1"到您的 TextView xml。

于 2012-08-26T20:11:32.087 回答
0

你可以这样做:

添加android:onClick="randomGenerator"到您的按钮属性。然后在你的活动中。创建一个在单击按钮时将调用的函数。(我假设您想在单击按钮时执行随机生成代码。)

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];

}

希望有帮助。

于 2012-08-26T20:11:35.457 回答