为了账号安全,请及时绑定邮箱和手机立即绑定

虽然循环在 Android 上不起作用

虽然循环在 Android 上不起作用

跃然一笑 2021-08-06 10:01:46
我需要以 1 秒的延迟显示数字。如果没有 while 循环,它可以工作,但是一旦我添加了 while 循环,它就会在 while 循环结束后打印值。有人可以在这方面提供帮助。int state = 0;int count=0;@Overrideprotected 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++);            }        }    });}
查看完整描述

3 回答

?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

永远不要调用Thread.sleep()UI 线程。您需要将代码放在一个单独的线程中,并使用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();

}


查看完整回答
反对 回复 2021-08-06
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

不要使用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** )


查看完整回答
反对 回复 2021-08-06
?
Helenr

TA贡献1780条经验 获得超4个赞

当您进行与时间相关的操作时,更好的方法是使用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();


    }


查看完整回答
反对 回复 2021-08-06
  • 3 回答
  • 0 关注
  • 139 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信