android按钮启动2个计时器

时间:2014-10-02 13:25:56

标签: android timer

我想在按下一个开始按钮时启动2个倒数计时器。 按下start时,maxTimer设置为minTimer的值,它们都以相同的值倒计时。 我如何分开2?我需要minTimer和maxTimer的倒计时功能。

package com.xpand.timer;

import java.util.concurrent.TimeUnit;

import android.support.v7.app.ActionBarActivity;
import com.google.android.gms.ads.*;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")

public class MainActivity extends ActionBarActivity implements OnClickListener {

    TextView minTimer, maxTimer, tv_start, tv_stop;
    Button btn_Timer1, btn_Timer2;
    int minTid, maxTid;

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


    minTimer = (TextView) findViewById(R.id.minTimer);
    minTimer.setText("00:00:00");
    maxTimer = (TextView) findViewById(R.id.maxTimer);
    maxTimer.setText("00:00:00");
    tv_start = (TextView) findViewById(R.id.tv_start);
    tv_stop = (TextView) findViewById(R.id.tv_stop);
    btn_Timer1 = (Button) findViewById(R.id.btn_Timer1);
    btn_Timer2 = (Button) findViewById(R.id.btn_Timer2);


    btn_Timer1.setOnClickListener(this);
    btn_Timer2.setOnClickListener(this);

    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")

    public class CounterClass extends CountDownTimer {

        public CounterClass(long millisInFuture, long countDownInterval){
            super (millisInFuture, countDownInterval);
        }
        @SuppressLint({ "NewApi", "DefaultLocale" })
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
            long millis = millisUntilFinished;

            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            String hms2 = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(hms);
            System.out.println(hms2);
            minTimer.setText(hms);
            maxTimer.setText(hms2);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            minTimer.setText("Completed");
            maxTimer.setText("Completed");
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()){

        case R.id.btn_Timer1:
            minTid=10000;
            maxTid=20000;
            minTimer.setText("00:00:10");
            maxTimer.setText("00:00:20");
            break;

        case R.id.btn_Timer2:
            minTid=15000;
            maxTid=30000;
            minTimer.setText("00:00:15");
            maxTimer.setText("00:00:30");
            break;  


        }

        final CounterClass timer = new CounterClass(minTid, 1000);
        tv_start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                timer.start();
            }




    });
        tv_stop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                timer.cancel();
            }

        });
    }
}

1 个答案:

答案 0 :(得分:1)

你需要创建2个CounterClass计时器......

final CounterClass timer = new CounterClass(minTid, 1000);
  final CounterClass timer2 = new CounterClass(maxTid, 1000);
        tv_start.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                timer.start();
                timer2.start()
            }




    });
        tv_stop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                timer.cancel();
                timer2.cancel()

            }

        });
    }