private Button mfactbutton;
private TextView mfacttext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_fun_fact);
Button mfactbutton = (Button) findViewById(R.id.button);
TextView mfacttext = (TextView) findViewById(R.id.textView2);
// now we need to make out button to click
View.OnClickListener Listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] facts = {
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built." };
String fact = "";
// randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(facts.length);
fact = facts[randomNumber] + "";
}
};
mfactbutton.setOnClickListener(Listener);
}
大家好!我需要幫助!我的按鈕不僅僅是簡單的! heeeeeeeeelp!我只是試圖做一個簡單的按鈕,每次點擊改變textview2!起初它工作,但現在它開始不工作。
因爲你沒有用'mfacttext'做任何事情,所以在這行之後加上'mfacttext.setText(fact)''fact = facts [randomNumber] +「」;' –
@Pavneet_Singh究竟是什麼意思 –