创建一个按钮

时间:2012-08-26 20:05:58

标签: android button

我正在尝试创建一个启动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 个答案:

答案 0 :(得分: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"
...

答案 1 :(得分: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。

答案 2 :(得分: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];

}

希望有所帮助。

答案 3 :(得分: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。即

<TextView
    android:id="textview1"
...