在我的应用中,我想要一个随机按钮每秒消失。
我现在的状态是,我的倒计时从30秒到0,每次按下一个按钮就会消失,另一个会显示。现在我想自动化,即每秒一个随机按钮消失而另一个按钮显示。这是我的代码,直到现在:
tv = (TextView) findViewById(R.id.tv);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("F " + millisUntilFinished / 1000);
System.out.println("HHH");
}
public void onFinish() {
tv.setText("done!");
Toast t = Toast.makeText(MainActivity.this, "Ihr Highscore: " + count, Toast.LENGTH_LONG);
t.show();
}
}.start();
每次用户按下按钮时都会调用setNextButton方法,我现在想要的是每秒自动调用一次:
public void setNextButton(Button str){
System.out.println("&&&&&&&&&&& SETNEXTBUTTON");
str.setVisibility(ImageButton.GONE);
int zufall = (int) (Math.random()*23);
int buttonid = str.getId();
int buttonname = (int) str.getId();
String buttonneu = "button" + zufall;
System.out.println("&&&&&&&&&&&" + getResources().getResourceEntryName(buttonid));
int[] buttonIds = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7, R.id.button8,
R.id.button9, R.id.button10, R.id.button11, R.id.button12, R.id.button13, R.id.button14, R.id.button15, R.id.button16 ,
R.id.button17, R.id.button18, R.id.button19, R.id.button20, R.id.button21, R.id.button22, R.id.button23, R.id.button24 };
Button bnew = (Button) findViewById(buttonIds[zufall]);
//Toast.makeText(this, "$ " + getResources().getResourceEntryName(bnew.getId()), Toast.LENGTH_LONG).show();
if(bnew.getId() == str.getId()){
bnew = (Button) findViewById(buttonIds[zufall]);
}
bnew.setVisibility(ImageButton.VISIBLE);
count++;
//b.setEnabled(enabled)
}
如何以一种好的方式解决这个问题?