简体   繁体   中英

android button starts 2 timers

I want to start 2 countdown timers when pressing one start button. When pressing start, maxTimer is set to the value of minTimer and they both count down with the same value. How do I seperate the 2? I need a countdown function for both minTimer and 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();
            }

        });
    }
}

you need to create 2 CounterClass timers...

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()

            }

        });
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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