0

我有一个简单的 Android 倒数计时器,其输出失真。我正在尝试创建一个简单的游戏,用户必须重复单击一个按钮,然后通过 8 个图像的过程将可绘制图像从一个更改为另一个。可绘制对象渲染得很好,但倒数计时器(应该从 30 秒开始)似乎会粘住和/或生成不代表当前经过的时间量的不正确数字。有没有人对为什么会发生这种情况或我应该调查什么来解决这个问题有任何建议?

MultitapGame.java

    public class MultitapGame extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.multitapgame);

    final View btn = findViewById(R.id.gameButton);
    final View img = findViewById(R.id.game1);

    final TextView mTextField = (TextView) findViewById(R.id.gameCDT);
    final TextView mTextField2 = (TextView) findViewById(R.id.gameCDT2);
    final View mTextField3 = findViewById(R.id.gameCDT3);
    final View mTextField4 = findViewById(R.id.gameCDT4);


    img.setBackgroundResource(R.drawable.image1);

        btn.setOnClickListener(new OnClickListener() {

            public int count = 0;


        @Override
        public void onClick(View v) {

            setCount(getCount() + 1);
            new CountDownTimer(30000, 1000) {

                 public void onTick(long millisUntilFinished) {

                     int timeLeft =  (int) (millisUntilFinished / 1000) ;

                     ((TextView) mTextField).setText("seconds remaining: ");
                     final TextView mTextField2 = (TextView) findViewById(R.id.gameCDT2);
                        mTextField2.setText(String.valueOf(timeLeft + ""));

                 }

                 public void onFinish() {

                     int numTaps = count;
                     if(numTaps<150)
                     {
                         ((TextView) mTextField3).setText("Unlucky chucky! ");
                         ((TextView) mTextField4).setText("taps done: " + numTaps);
                     }
                     else
                     {
                         ((TextView) mTextField3).setText("Congrats Bro :-) !");
                         ((TextView) mTextField4).setText("taps done: " + numTaps);
                     }
                 }
              }.start();


            if(count==15)
            {
            img.setBackgroundResource(R.drawable.image2);}
            if(count==30)
            {
            img.setBackgroundResource(R.drawable.image3);}
            if(count==45)
            {
            img.setBackgroundResource(R.drawable.image4);}
            if(count==60)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==75)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==90)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==100)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==110)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==115)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==118)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==121)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==123)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==125)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==126)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==127)
            {
            img.setBackgroundResource(R.drawable.image6);}

            if(count==130)
            {
            img.setBackgroundResource(R.drawable.image7);}
            if(count==140)
            {
            img.setBackgroundResource(R.drawable.image1);}
            if(count==160)
            {
                Intent i = new Intent();
                i.setClassName("com.B00512756.angertwo",
                        "com.B00512756.angertwo.Strategies");
                startActivity(i);
                finish();}

        }


        public int getCount() {
            return count;
        }


        public void setCount(int count) {
            this.count = count;
        }
        });
}

}

4

1 回答 1

1

我认为问题在于在 onClick 方法中实现 CountDownTimer 。

我建议将其实现为扩展 CountDownTimer 的子类,例如:

public class MyCounter extends CountDownTimer{

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

                 @Override
                 public void onTick(long millisUntilFinished) {

                     mTextField.setText("seconds remaining: ");
                     mTextField2.setText(String.valueOf((int) (millisUntilFinished/1000) + ""));

                 }

                 @Override
                 public void onFinish() {

                     int numTaps = count;
                     if(numTaps<150)
                     {
                         mTextField3.setText("Unlucky chucky! ");
                         mTextField4.setText("taps done: " + numTaps);
                     }
                     else
                     {
                         mTextField3.setText("Congrats Bro :-) !");
                         mTextField4.setText("taps done: " + numTaps);
                     }
                 }

    }

此外,最好使 onTick() 中的代码尽可能短,因为对的调用onTick(long)与该对象同步。因此onTick(long),在前一个回调完成之前不会发生一次调用。仅当执行onTick(long)所需的时间与倒计时间隔相比显着时,这才有意义。因此,如果您的onTick()代码执行时间超过一秒,您的倒计时就会出现滞后。这就是为什么我把它缩短了。

最好在您的内部初始化您的 TextViewonCreate()并将它们声明为公共类变量,这样您就不必在每次计数器滴答或完成时都这样做。

下一步是初始化你的计时器onCreate

final MyCounter timer = new MyCounter(30000,1000);

最后,您需要在您的onClick()方法中启动它:

timer.start();
于 2012-07-09T04:32:53.617 回答