有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在安卓中一个接一个地运行计时器?

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

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 安卓 class
    private CountDownTimer countDownTimer1; // built in 安卓 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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:id="@+id/LinearLayout1"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:orientation="vertical" >

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

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

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

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

    </LinearLayout>

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

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

    </LinearLayout>

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

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

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

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

    </LinearLayout>

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

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

    </LinearLayout>

</LinearLayout>

共 (0) 个答案