本文介绍了TextView不同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个应用程序和简单游戏,类似于带有数字的MasterMind.我希望当个人电脑尝试找到我的号码时,不要立即显示他在找到正确号码之前所做的所有尝试,而要逐步显示出来.例如,CPU说出第一个数字,等待一秒钟,然后说出第二个数字,依此类推.这是代码:

I'm building my first App and simple game similar to MasterMind with numbers. I would that when the pc try to find my number, don't show immediately all the attempts that he does before finding the correct number, but that he does it gradually. For example the CPU say the first number, wait one second and then say the second number and so on. Here is the code:

public void startCPU() {
    int num;
    int strike;
    int xx;

    do {
        do {
            Random random = new Random();
            num = random.nextInt((9876 - 123) + 1) + 123;
            xx = (int) numpos.pox[num];
        } while (xx == 0);

        Numeri.Confronta(mioNumero, numpos.pox[num]);

        int ball = Risultati.getBall();
        strike = Risultati.getStrike();
        TextView txtv = (TextView) findViewById(R.id.numberthatcpusay);

        if (numpos.pox[num] < 1000) {
            Integer x = numpos.pox[num];
            String s = ("0" + Integer.toString(x) + " " + Integer.toString(strike) + "X "
                    + Integer.toString(ball) + "O" + "  ");
            txtv.append(s);

        } else {
            Integer x = numpos.pox[num];
            String s = (Integer.toString(x) + " " + Integer.toString(strike) + "X "
                    + Integer.toString(ball) + "O" + "   ");
            txtv.append(s);
        }

        numeroDato = numpos.pox[num];
        numpos.pox[num] = 0;
        for (int i = 0; i <= 9876; i++) {
            if (Risultati.checkStrikeAndBall(numeroDato, numpos.pox[i], strike, ball) == true) {
                numpos.pox[i] = 0;
            }
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    } while (strike != 4);

    startMe();
}

问题在于textView不会每秒更新一次,但只有在CPU找到编号后才会更新.这样,当我单击启动此方法的按钮时,在看到结果等于尝试次数之前应该等待的时间.例如,如果CPU在找到正确的数字之前需要9个数字,我应该等待9秒钟,然后textView显示所有尝试.我该如何解决?

The problem is that the textView don't update every second but he update only when the CPU have found the number. In this way when I click on the button that launch this mehtod the time that I should wait before i see the results is equal at the number of the attemps. For example, if the CPU needs 9 number before he found the correct number I should wait 9 seconds before the textView show all the attempts. How can I resolve it?

推荐答案

您不能在UI线程上做到这一点!

You can't do that on the UI thread!

您正在运行一个循环,该循环在UI线程上调用Thread.sleep(),这基本上会完全冻结电话.

You're running a loop that calls Thread.sleep() on the UI thread, which basically freezes the phone completely.

考虑使用 AsyncTask .

不在UI线程上,因此可以调用sleep.然后每秒计算新数字并调用publishProgress来更新用户界面.然后在onProgressUpdate回调中,执行TextView.append()调用-因为必须在UI线程上调用它.

in the doInBackground method you're not on the UI thread, so you can call sleep.and every second calculate the new number and call publishProgress to update the UI.Then in the onProgressUpdate callback, you do the TextView.append() call - because it must be called on the UI thread.

这篇关于TextView不同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:19