我想創建一個菜單,以隨機順序在視圖片快照的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