StartTimer()应该在按钮点击事件完成后调用,但是在onclick事件之前执行它

时间:2016-07-23 12:44:50

标签: android android-studio

StartTimer()函数应该在按钮点击事件完成后调用,但是在onclick事件之前执行它。如何在onclick事件之前停止计时器并在onclick事件之后启动。

我该如何解决这个问题?

        btn1=(Button)findViewById(R.id.button);
        btn2=(Button)findViewById(R.id.button2);
        btn3=(Button)findViewById(R.id.button3);
        timer = new Timer();

        btn1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
            }
        });
        StartTimer();

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
            }
        });
        StartTimer();

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void StartTimer()
    {
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                Log.v("timer", "Timer running");
            }
        }, 0, 5000);

        Toast.makeText(getApplicationContext(), "Timer", Toast.LENGTH_LONG).show();
    }

    protected void onResume() {
        super.onResume();
        StartTimer();
    }
}

1 个答案:

答案 0 :(得分:0)

android中已经内置了Timer类。所以你可以这样做:

    import java.util.Timer;

    Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.v("timer", "Timer running");
                Toast.makeText(getApplicationContext(), "TIMER HAS FINISHED", Toast.LENGTH_SHORT).show();
            }
        }, 0, 5000);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Toast.makeText(getApplicationContext(), "Button1", Toast.LENGTH_SHORT).show();
            }
        });

        StartTimer();

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Toast.makeText(getApplicationContext(), "Button2", Toast.LENGTH_SHORT).show();
            }
        });

        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
                Toast.makeText(getApplicationContext(), "Button3", Toast.LENGTH_SHORT).show();
            }
        });

所以只需在onCreate中启动计时器,并在点击按钮时停止计时器。