切换按钮启动Timer1和Timer2但仅停止Timer1

时间:2015-11-09 20:20:41

标签: java android timer toggle togglebutton

我试图说服一个Toggle按钮在单击“Start Timer”时启动Timer1和Timer2并且只停止Timer1使Timer2继续“Stop Timer” 我的主要活动看起来像这样:

StoredProcedureQuery storedProcedureQuery = entityManagerFactory.createEntityManager().createStoredProcedureQuery("ModelSearchByModel",ModelSearchByModelEntity.class);

storedProcedureQuery.registerStoredProcedureParameter("RegionCode", String.class , ParameterMode.IN);
// register all the other parameters...

storedProcedureQuery.setParameter("RegionCode", regionCode);
// set all the other parameters...

return storedProcedureQuery.execute() ? storedProcedureQuery.getResultList() : null;

目标是当切换“START TIMER”时,单击TIMERS(chrononeter1和chronometer2)应该启动,当点击“STOP TIMER”时,只有chronometer1应该停止。 请指教!谢谢!

XML看起来像这样:

public class MainActivity extends AppCompatActivity {

    long init, now, paused, millis, seconds, minutes, hours;
    TextView display;
    TextView display2;
    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        handler = new Handler();
        display = (TextView) findViewById(R.id.chronometer1);
        final ToggleButton passTog = (ToggleButton) findViewById(R.id.toggleButton1);
        final Runnable updater = new Runnable() {
            @Override
            public void run() {
                if (passTog.isChecked()) {
                    now = System.currentTimeMillis();
                    millis = now - init;
                    seconds = ((millis / 1000) % 60);
                    minutes = (millis / 1000) / 60;
                    hours = (minutes / 3600000);
                    String hh = hours < 10 ? "0" + hours : hours + "";
                    String mm = minutes < 10 ? "0" + minutes : minutes + "";
                    String ss = seconds < 10 ? "0" + seconds : seconds + "";
                    display.setText(hh + ":" + mm + ":" + ss);
                    handler.postDelayed(this, 10);
                    continueTimer();
                }
            }
        };


        passTog.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                init = System.currentTimeMillis();
                handler.post(updater);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        paused = System.currentTimeMillis();

    }

    @Override
    protected void onResume() {
        super.onResume();
        init += System.currentTimeMillis() - paused;

    }


    public void continueTimer() {
        display2 = (TextView) findViewById(R.id.chronometer2);
        now = System.currentTimeMillis();
        millis = now - init;
        seconds = ((millis / 1000) % 60);
        minutes = (millis / 1000) / 60;
        hours = (minutes / 3600000);
        String hh = hours < 10 ? "0" + hours : hours + "";
        String mm = minutes < 10 ? "0" + minutes : minutes + "";
        String ss = seconds < 10 ? "0" + seconds : seconds + "";
        display2.setText(hh + ":" + mm + ":" + ss);
    }

1 个答案:

答案 0 :(得分:1)

好的,一直看着你的代码。你可以使用天文台表。 像:

//Decalare and initialize timer1.
private Chronometer timer1;
timer1 = (Chronometer)v.findViewById(R.id.chronometer1);

//Declare and initialize timer2.  
private Chronometer timer2;
timer2 =(Chronometer)v.findViewById(R.id.chronometer2);

然后在切换按钮中启动它们:

@Override
public void run() {
    if (passTog.isChecked()) {
        //start chronometers
        timer1.start();
        timer2.start();
    }

然后当passTog.isChecked()= false时,只调用timer1.stop()。第二个计时器将继续。

换句话说,当检查切换时,在两个计时器上调用.start(),并且当取消选中切换时,只在一个计时器上调用.stop()。

我没有看到&#34;停止计时器&#34;代码。