我想创建一个菜单,以随机顺序在视图片快照的5个孩子之间以随机时间间隔“翻转”。ViewFlipper:使用随机孩子随机时间间隔翻转
我试过下面的代码,我可以让System.out.println显示我的调试消息,以随机时间间隔记录在logcat中,这样就可以工作。 但是,我的模拟器屏幕全是黑色的。
当我在固定int的“onCreate”方法中使用setDisplayedChild方法时,它工作正常。你能帮助我吗?非常感谢!
public class FlipperTest extends Activity {
int randomTime;
int randomChild;
ViewFlipper fliptest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_beat_the_game);
ViewFlipper fliptest = (ViewFlipper) findViewById(R.id.menuFlipper);
//this would work
//fliptest.setDisplayedChild(3);
while (true){
try {
Thread.sleep(randomTime);
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
Random timerMenu = new Random();
randomTime = timerMenu.nextInt(6) * 2000;
Random childMenu = new Random();
randomChild = childMenu.nextInt(5);
fliptest.setDisplayedChild(randomChild);
System.out.println("executes the finally loop");
}
}
}
感谢许多Luksprog!这非常有帮助!我会在最后的代码下面发帖并解释一下。无论如何,你摇滚! – user2595866