简体   繁体   English

TextView不同步

[英]TextView doesn't synchronize

I'm building my first App and simple game similar to MasterMind with numbers. 我正在用数字制作类似于MasterMind的第一个应用程序和简单游戏。 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. 例如,CPU说出第一个数字,等待一秒钟,然后说出第二个数字,依此类推。 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();
}

The problem is that the textView don't update every second but he update only when the CPU have found the number. 问题在于textView不会每秒更新一次,而只有在CPU找到编号后才会更新。 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. 例如,如果CPU需要9个数字才能找到正确的数字,我应该等待9秒钟,然后textView显示所有尝试。 How can I resolve it? 我该如何解决?

You can't do that on the UI thread! 您不能在UI线程上执行此操作!

You're running a loop that calls Thread.sleep() on the UI thread, which basically freezes the phone completely. 您正在运行一个循环,该循环在UI线程上调用Thread.sleep() ,这基本上将手机完全冻结。

Consider using an AsyncTask . 考虑使用AsyncTask

in the doInBackground method you're not on the UI thread, so you can call sleep. doInBackground方法中,您不在UI线程上,因此可以调用sleep。 and every second calculate the new number and call publishProgress to update the UI. 然后每秒计算新数字并调用publishProgress来更新UI。 Then in the onProgressUpdate callback, you do the TextView.append() call - because it must be called on the UI thread. 然后在onProgressUpdate回调中,执行TextView.append()调用-因为必须在UI线程上调用它。

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

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