循环和延迟问题 [英] loop and delay problem

查看:98
本文介绍了循环和延迟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个小程序,模拟骰子。我想骰子侧面的图片点击按钮后随意改变的6倍。它应该每次随机改变后,随意改变与0,3秒的延迟的6倍。问题是,它总是改变只有一次没有六倍希望。我想这将只是一些琐碎的错误,但我不能在网络上的任何地方找到正确的答案。这里是code和我感谢ü所有提前:

 导入了java.util.Random;进口android.app.Activity;
进口android.os *。
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.ImageView;公共类OneDice延伸活动{    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.onedicelayout);
        最终ImageView的图像=(ImageView的)findViewById(R.id.OneDiceRollImage);
        最终诠释[] = arrayOfDices {
                R.drawable.icon,
                R.drawable.icon2,
                R.drawable.icon3,
        };
        最后随机兰特=新的随机();        View.OnClickListener rollOneDiceListener =新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                INT J = 0;
                的for(int i = 0;我6;;我++){
                    尝试{
                        J = rand.nextInt(3);
                        image.setImageResource(arrayOfDices [J]);
                        视频下载(300);
                    }赶上(InterruptedException的E){
                        e.printStackTrace();
                    }
                }
            }
        };        按钮oneDiceRollButton =(按钮)findViewById(R.id.OneDiceRollButton);
        oneDiceRollButton.setOnClickListener(rollOneDiceListener);    }}


解决方案

这是通过你的骰子影像界的onClick 的方法是从系统的回调。
这意味着它调用回调UI线程只会再次操作方法返回时。并且系统没有记录您在内部做什么,但只是走过去的状态(即最后选定的图像)。

您可以解决这个问题了的AsyncTask ,你必须 doInBackground()(伪code):

 的for(int i = 0;我6;;我++){
   publishProgress(randomNumber);
   视频下载(300);
}

和内 onProgressUpdate()则可以显示图像

I am trying to make a little application that simulates the dice. I want the picture of the dice side randomly change 6 times after clicking the button. It should randomly change 6 times with 0,3 second delay after every random change. The problem is that it changes always only one time not six times as wished. I guess it will be just some trivial mistake but I could not find the right answer anywhere on the web. Here is the code and I thank u all in advance:

   import java.util.Random;

import android.app.Activity;
import android.os.*;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class OneDice extends Activity{

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onedicelayout);


        final ImageView image = (ImageView) findViewById(R.id.OneDiceRollImage);
        final int[] arrayOfDices = {
                R.drawable.icon,
                R.drawable.icon2,
                R.drawable.icon3,
        };
        final Random rand = new Random();

        View.OnClickListener rollOneDiceListener = new View.OnClickListener() {

            @Override
            public void onClick(View v){
                int j = 0;
                for(int i = 0; i<6; i++){
                    try {
                        j = rand.nextInt(3);
                        image.setImageResource(arrayOfDices[j]);
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Button oneDiceRollButton = (Button) findViewById(R.id.OneDiceRollButton);
        oneDiceRollButton.setOnClickListener(rollOneDiceListener);

    }

}

解决方案

The onClick method that circles through your dice images is a callback from the system. Meaning that the UI thread which calls the callback will only operate again when the method returns. And the system is not recording what your are doing internally, but just taking the last state (i.e. the last selected image).

You can solve this with an AsyncTask where you have doInBackground() (pseudo code):

for (int i = 0; i< 6 ; i++) {
   publishProgress(randomNumber);
   Thread.sleep(300);
}

And within onProgressUpdate() you can then display the image

这篇关于循环和延迟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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