如何在android中一个接一个地运行计时器?

时间:2017-03-14 10:41:58

标签: java android timer countdowntimer android-timer

如何一个接一个地运行计时器? 例如:我使用两个edittext和一个按钮, 在输入2 edittext为1和2(以分钟为单位)后,计时器应首先完成第一个给定输入,然后开始第二个输入。 然后当我点击停止计时器时,它应该停止并显示edittext输入和停止的计时器值。 但它仅适用于第一次输入..但在第二次输入时失败。 这是我试过的代码。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A1887F"
android:padding="20dp"
android:orientation="horizontal">

<TextView
    android:text="1"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceMedium"
    android:background="#4CAF50"
    android:padding="10dp"
    android:textColor="#ffffff"
    android:layout_marginEnd="10dp"
    />

<TextView
    android:text="2"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:textAppearance="?android:textAppearanceMedium"
    android:background="#4CAF50"
    android:padding="10dp"
    android:textColor="#ffffff"
    android:layout_marginStart="10dp"
    />

布局就是这样。

public class MainActivity extends Activity implements OnClickListener {

    private Button buttonStartTime, buttonStopTime;
    private EditText edtTimerValue;
    private EditText edtTimerValue1;
    private TextView textViewShowTime; // will show the time
    private TextView textViewShowTime1; // will show the time
    private CountDownTimer countDownTimer; // built in android class
    private CountDownTimer countDownTimer1; // built in android class
    // CountDownTimer
    private long totalTimeCountInMilliseconds; // total count down time in
    private long totalTimeCountInMilliseconds1;
    // milliseconds
    private long timeBlinkInMilliseconds; // start time of start blinking
    private long timeBlinkInMilliseconds1;
    private boolean blink; // controls the blinking .. on and off
    private boolean blink1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonStartTime = (Button) findViewById(R.id.btnStartTime);
        buttonStopTime = (Button) findViewById(R.id.btnStopTime);
        textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
        edtTimerValue = (EditText) findViewById(R.id.edtTimerValue);


        buttonStartTime.setOnClickListener(this);
        buttonStopTime.setOnClickListener(this);

        textViewShowTime1 = (TextView) findViewById(R.id.tvTimeCount1);
        edtTimerValue1 = (EditText) findViewById(R.id.edtTimerValue2);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnStartTime) {
            textViewShowTime.setTextSize(15);
            setTimer();
            buttonStopTime.setVisibility(View.VISIBLE);
            buttonStartTime.setVisibility(View.GONE);
            edtTimerValue.setVisibility(View.GONE);
            edtTimerValue.setText("");
            startTimer();

        } else if (v.getId() == R.id.btnStopTime) {
            countDownTimer.cancel();
            if(countDownTimer1!=null) {
                countDownTimer1.cancel();
            }
            buttonStartTime.setVisibility(View.VISIBLE);
            buttonStopTime.setVisibility(View.GONE);
            edtTimerValue.setVisibility(View.VISIBLE);
        }
    }

    private void setTimer() {
        int time = 0;
        if (!edtTimerValue.getText().toString().equals("")) {
            time = Integer.parseInt(edtTimerValue.getText().toString());
        } else
            Toast.makeText(MainActivity.this, "Please Enter Minutes...",
                    Toast.LENGTH_LONG).show();

        totalTimeCountInMilliseconds = 60 * time * 1000;

        timeBlinkInMilliseconds = 30 * 1000;
    }

    private void startTimer() {
        countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {

            @Override
            public void onTick(long leftTimeInMilliseconds) {
                long seconds = leftTimeInMilliseconds / 1000;

                if (leftTimeInMilliseconds < timeBlinkInMilliseconds) {

                    if (blink) {
                        textViewShowTime.setVisibility(View.VISIBLE);
                    } else {
                        textViewShowTime.setVisibility(View.INVISIBLE);
                    }

                    blink = !blink; // toggle the value of blink
                }

                textViewShowTime.setText(String.format("%02d", seconds / 60)
                        + ":" + String.format("%02d", seconds % 60));
            }

            @Override
            public void onFinish() {
                // this function will be called when the timecount is finished
                textViewShowTime.setText("Time up!");
                textViewShowTime.setVisibility(View.VISIBLE);
                buttonStartTime.setVisibility(View.VISIBLE);
                buttonStopTime.setVisibility(View.GONE);
                edtTimerValue.setVisibility(View.VISIBLE);
                setTimer1(); //here i am using two call the second input
                startTimer1(); //here i am using to start the second input automatically.
            }

        }.start();

    }
//this is not working..
    private void setTimer1() {
        Log.e("text","tesxt");
        int time = 0;
        if (!edtTimerValue1.getText().toString().equals("")) {
            time = Integer.parseInt(edtTimerValue1.getText().toString());
        } else
            Toast.makeText(MainActivity.this, "Please Enter Minutes...",
                    Toast.LENGTH_LONG).show();

        totalTimeCountInMilliseconds1 = 60 * time * 1000;

        timeBlinkInMilliseconds1 = 30 * 1000;
    }
 //this function is also not working to start the second input..
    private void startTimer1() {
        Log.e("time","time");
        countDownTimer1 = new CountDownTimer(timeBlinkInMilliseconds1, 500) {

            @Override
            public void onTick(long millisUntilFinished) {
                long seconds = millisUntilFinished / 1000;

                if (millisUntilFinished < timeBlinkInMilliseconds) {
                    if (blink1) {
                        textViewShowTime1.setVisibility(View.VISIBLE);
                    } else {
                        textViewShowTime1.setVisibility(View.INVISIBLE);
                    }

                    blink1 = !blink1; 
                }

                textViewShowTime1.setText(String.format("%02d", seconds / 60)
                        + ":" + String.format("%02d", seconds % 60));
            }

            @Override
            public void onFinish() {
                textViewShowTime1.setText("Time up!");
                textViewShowTime1.setVisibility(View.VISIBLE);
                edtTimerValue1.setVisibility(View.VISIBLE);
            }
        }.start();
    }
}

0 个答案:

没有答案