通过开始新活动,它会一次又一次地开始

时间:2014-03-24 13:14:21

标签: android android-intent start-activity

我设置了一个textview,用于定时器功能。 当计时器达到我想要的时间时,它应该开始一个新的活动,将我的布局设置为另一个。

textfield=(TextView)findViewById(R.id.TVTimer);
    handler = new Handler();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            while (Running){
                try {

                    Thread.sleep(1000);
                    }

                catch (InterruptedException e) {
                    e.printStackTrace();
                    // TODO: handle exception
                }
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        number+=1;
                        textfield.setText(String.valueOf(number));
                        if (number>5) {
                            Intent intent = new Intent(getApplicationContext(), Uebung2.class);
                            startActivity(intent);

                        }

                    }
                } );
            }

        }
    };
    new Thread(runnable).start();

这是被叫类:

   public class Uebung2 extends Oberklasse {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //Liegestützen1(2);
    setContentView(R.layout.new);

}

}

现在我在计时器到达时间之后遇到了问题,我想要的,我的应用程序每隔一秒又一次又一次地设置布局。 设置布局一次的解决方案是什么?

2 个答案:

答案 0 :(得分:2)

因为你有这个

 handler.post(new Runnable() {
                @Override
                public void run(){
                    number+=1;
                    textfield.setText(String.valueOf(number));
                    if (number>5) {
                        Intent intent = new Intent(getApplicationContext(), Uebung2.class);
                        startActivity(intent);

                    }

                }
            } );

在你的while循环中。

还要查看您的号码"值。

尝试使用while循环

 int i=1000;
 while (i<5000){
            try {

                Thread.sleep(i);
                i+=1000;
                }

            catch (InterruptedException e) {
                e.printStackTrace();
                // TODO: handle exception
            }
         }

然后执行你的runnable。

答案 1 :(得分:0)

使用此

textfield=(TextView)findViewById(R.id.TVTimer);
boolean loop = true;

handler = new Handler();
Runnable runnable = new Runnable() {

    @Override
    public void run() {
        while (loop){
            try {

                Thread.sleep(1000);
                }

            catch (InterruptedException e) {
                e.printStackTrace();
                // TODO: handle exception
            }
            handler.post(new Runnable() {
                @Override
                public void run(){
                    number+=1;
                    textfield.setText(String.valueOf(number));
                    if (number>5) {
                        loop = false; // this will end loop
                        Intent intent = new Intent(getApplicationContext(), Uebung2.class);
                        startActivity(intent);

                    }

                }
            } );
        }

    }
};
new Thread(runnable).start();
相关问题