在计时器运行时,在进一步输入时重置android计时器时出错

时间:2016-03-31 17:18:09

标签: java android timer

当我启动一个提供输入的新计时器时,此代码正在运行。但是每当我在前一个输入停止之前给出另一个输入时,两个计时器都会一次运行。我想在给出第二个输入时重置前一个计时器。我怎么能在这里做到?

MainActivity.java

package com.example.asifsabir.counterapps;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    TextView tv1;
    EditText et1;
    Button bt1;
    int sec=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.textView1);
        et1 =(EditText) findViewById(R.id.et1);
        bt1= (Button) findViewById(R.id.bt1);

        bt1.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        int min = Integer.parseInt(et1.getText().toString());

                        if (!et1.getText().toString().isEmpty()) {
                            sec = min * 60 * 1000;
                            final CountDownTimer myC = new CountDownTimer(sec, 1000) {

                                public void onTick(long millisUntilFinished) {
                                    tv1.setText("" + String.format("%02d:%02d",
                                            TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
                                                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                                }

                                public void onFinish() {
                                    tv1.setText("done!");
                                }
                            };
                        }
                    }
                }
        );
    }

}

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">


    <EditText
        android:layout_marginTop="100dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/et1"
        android:textColor="@android:color/holo_red_light"
        android:shadowColor="@android:color/holo_red_light"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:hint="enter minute"
        android:inputType="number"
        android:textAlignment="center"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt1"
        android:background="@android:color/holo_red_light"
        android:layout_gravity="center"
        android:text="submit"/>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/button_shape"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:weightSum="1">

    <TextView
        android:textAlignment="center"
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="count"
        android:gravity="center"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:textColor="@android:color/holo_red_light"
        android:textSize="80sp" />


    </LinearLayout>

    </LinearLayout>

0 个答案:

没有答案