繁体   English   中英

虽然循环在 Android 上不起作用

[英]While loop doesn't work on Android

我需要以 1 秒的延迟显示数字。 如果没有 while 循环,它可以工作,但是一旦我添加了 while 循环,它就会在 while 循环结束后打印值。 有人可以在这方面提供帮助。

int state = 0;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button=(Button)findViewById(R.id.b_main);
    textView =(TextView) findViewById(R.id.text_main);
    listView =(ListView) findViewById(R.id.t_main);
    arrayList=new ArrayList<String>();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(state==0){
                state=1;
                try {
                    while(count<10) {
                        textView.setText("Start " + count++);
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else{
                state=0;
                textView.setText("Stop " + count++);
            }
        }
    });
}

永远不要在 UI 线程上调用Thread.sleep() 您需要将代码放在一个单独的线程中,并使用runOnUiThread发布结果:

if(state==0){
    state=1;

    new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                while(count<10) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("Start " + count++);
                        }
                    });

                    Thread.sleep(1000);
                    count++;
                } 
            } catch (InterruptedException e) {
               e.printStackTrace();
            }
        }
    }).start();
}

不要使用Thread.sleep() 使用处理程序来强制执行您想要的延迟。

    final Handler handler = new Handler();
    final int delay1 = 1000; // adjust as needed


     handler.postDelayed(new Runnable() {
                public void run() {

                    // Code Block 1. Code here will be executed after 1000 millisec


                }
            }, delay1);


//  Code Block 2. Code here will be executed immediatelly ( before **Code Block 1** )

当您进行与时间相关的操作时,更好的方法是使用CountDownTimer 在主线程上使用Thread.sleep()是不好的,因为当线程休眠时你的 Ui 会卡住

这是一个关于如何实现它的示例CountDownTimer

int state = 0, count = 0;
    TextView textView;
    CountDownTimer timer;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        button=(Button)findViewById(R.id.b_main);
        textView =(TextView) findViewById(R.id.text_main);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state == 0) {
                    state = 1;

                    startTimer();

                } else {
                    state = 0;
                    if (timer != null) {
                        timer.cancel();
                    }
                    textView.setText("Stop " + count++);
                }
            }
        });
    }

    public void startTimer() {
        timer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                    count++;
                    textView.setText("Start " + count);
            }

            @Override
            public void onFinish() {
                       //10 sec finished
            }
        };

        timer.start();

    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM