ViewFlipper:采用随机儿童随机时间间隔翻动 [英] ViewFlipper: flipping at random time intervals using random children

查看:209
本文介绍了ViewFlipper:采用随机儿童随机时间间隔翻动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创造出5名儿童在随机顺序viewflipper之间的翻转随机时间间隔的菜单。

我尝试以下code和我可以得到的System.out.println来显示我的调试消息在随机时间间隔的logcat将被记录,这样的工作。
然而,我在模拟器的屏幕是全黑的。

当我只需在的onCreate方法用一个固定的INT使用setDisplayedChild方法,它工作正常。你能不能帮我这个?感谢很多!

 公共类FlipperTest延伸活动{INT randomTime;
INT randomChild;
ViewFlipper fliptest;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_beat_the_game);
    ViewFlipper fliptest =(ViewFlipper)findViewById(R.id.menuFlipper);            //这会工作
            //fliptest.setDisplayedChild(3);    而(真){
        尝试{
            视频下载(randomTime);        }赶上(InterruptedException的E){
            e.printStackTrace();
        }最后{
            随机timerMenu =新的随机();
            randomTime = timerMenu.nextInt(6)* 2000;
            随机childMenu =新的随机();
            randomChild = childMenu.nextInt(5);
            fliptest.setDisplayedChild(randomChild);            的System.out.println(执行最后循环);
        }
    }}


解决方案

像你千万不要阻塞UI线程的Thread.sleep()(+无限循环),而不是使用处理程序,例如,使您的翻转:

 私人ViewFlipper mFliptest;
私人处理程序mHandler =新的处理程序();
私人随机mRand =新的随机();
私人Runnable接口mFlip =新的Runnable(){    @覆盖
    公共无效的run(){
        mFliptest.setDisplayedChild(mRand.nextInt());
        mHandler.postDelayed(此,mRand.nextInt(6)* 2000);
    }
}//在onCreate方法
mFliptest =(ViewFlipper)findViewById(R.id.menuFlipper);
mHandler.postDelayed(mFlip,randomTime);

I want to create a menu which "flips" at random time intervals between 5 children of a viewflipper in random order.

I tried the following code and I can get the System.out.println to display my debugging message to be logged in the logcat at random time intervals so that's working. However, my screen in the emulator is all black.

When I simply use the setDisplayedChild method in the "onCreate" method with a fixed int, it works fine. Can you help me with this? Thanks many!

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");
        }
    }

}

解决方案

Don't block the UI thread like you do with Thread.sleep()(+ infinite loop), instead use a Handler, for example, to make your flips:

private ViewFlipper mFliptest;
private Handler mHandler = new Handler();
private Random mRand = new Random();
private Runnable mFlip = new Runnable() {

    @Override
    public void run() {
        mFliptest.setDisplayedChild(mRand.nextInt());
        mHandler.postDelayed(this, mRand.nextInt(6) * 2000);
    }    
}

//in the onCreate method
mFliptest = (ViewFlipper) findViewById(R.id.menuFlipper);
mHandler.postDelayed(mFlip, randomTime);

这篇关于ViewFlipper:采用随机儿童随机时间间隔翻动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆