这一直困扰着我,我是初学者,似乎无法解决这个问题。这是我的代码:
package com.example.imhungry;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class ImHungry extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_hungry);
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];
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_im_hungry, menu);
return true;
}
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];
}
基本上我试图让wordToDisplay显示在我的textView中,但我无法弄清楚如何这样做。
<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=""
tools:context=".ImHungry" />
另外,很抱歉提出这么多问题,我是新来的。谢谢!
答案 0 :(得分:0)
要让您的测试显示在TextView中,您必须使用setText();
进行设置,因此基本上您必须指定textview ID或标记(添加android:id="@+id/mytextview
),然后获取该对象TextView tv = (TextView)findViewById(R.id.mytextview); tv.setText("foo");
我还建议你阅读一些关于android基础知识的教程 - 会有所帮助
答案 1 :(得分:0)
将ID添加到TextView:
<TextView
android:id="@+id/text_random_text"
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=""
tools:context=".ImHungry" />
在代码中执行下一步:
在按钮后面添加:
final TextView textView = (TextView) findViewById(R.id.text_random_text);
将wordToDisplay设置为onClick(View v)方法中的文本
textView.setText(wordToDisplay);
答案 2 :(得分:0)
为提高效率,请尝试以下方法:
public class ImHungry extends Activity {
Random mRandomGenerator = new Random();
TextView mTextView;
String mWordList[] = new String[] {
"Mexican", "American", "Barbeque",
"Chinese", "Indian", "Italian",
"Thai", "Viatnamese", "Middle Eastern" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_hungry);
mTextView = (TextView) findViewById(R.id.textView);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
mTextView.setText(mWordList[mRandomGenerator.nextInt(9)]);
}
});
}
}
您可以删除其余代码。
只需创建一个随机数对象,如果只使用一次,则不需要将随机整数或相应的String保存在局部变量中。但是,如果要提高可读性,可以将它们保存在局部变量中。