0

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

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

布局是这样的。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="10dp" >

        <EditText
            android:id="@+id/edtTimerValue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="minutes"
            android:inputType="phone" />

        <Button
            android:id="@+id/btnStartTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Start Timer" />

        <Button
            android:id="@+id/btnStopTime"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Stop Timer"
            android:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvTimeCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00"
            android:textStyle="bold"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="10dp" >

        <EditText
            android:id="@+id/edtTimerValue2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="minutes"
            android:inputType="phone" />

        <Button
            android:id="@+id/btnStartTime2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Start Timer"
            android:visibility="gone"/>

        <Button
            android:id="@+id/btnStopTime2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:layout_weight="2"
            android:gravity="center"
            android:text="Stop Timer"
            android:visibility="gone" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvTimeCount1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00"
            android:textStyle="bold"/>

    </LinearLayout>

</LinearLayout>
4

0 回答 0