如何通过按钮启动和停止倒数计时器?

时间:2011-01-24 22:44:57

标签: android eclipse

这就是我到目前为止......它只是在应用程序打开时启动:

package com.android.countdown;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

public class countdown extends Activity {

    TextView mTextField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextField = (TextView) findViewById(R.id.timer1);

        new CountDownTimer(100000, 1000) {
            public void onTick(long millisUntilFinished) {
                mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextField.setText("Finished");
            }
        }.start();
    }
}

我知道我需要在按钮程序中调用start()。但是,如果我将.start()new CountDownTimer(100000 , 1000) {处的{{1}}移到错误处。

3 个答案:

答案 0 :(得分:15)

嗯......也许你需要先了解Java和编程是如何工作的。然后,您可以尝试执行以下操作:

CountDownTimer aCounter = new CountDownTimer(100000 , 1000) {
    public void onTick(long millisUntilFinished) {
        mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        mTextField.setText("Finished");
    }
};
aCounter.start();

答案 1 :(得分:0)

你可以这样做,以制作你想要的东西。这是Java代码:

package com.example.smartbroashad.countdowntimer;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
TextView timing;
TextView status;
int workingornot=1;
//running

public void startmeplease(View view)
{
    if (workingornot==1)
    {
        workingornot=2;
        //runned
        countDownTimerofme.start();
        Toast.makeText(this,"STARTED!",Toast.LENGTH_SHORT).show();
    }
    else
    {
        Toast.makeText(this,"already started!",Toast.LENGTH_SHORT).show();
    }

}
public void stopmeplease(View view)
{
    countDownTimerofme.cancel();
    status.setText("stoped!!");
    workingornot=1;
}
public void settimeplease(View view)
{

}
CountDownTimer countDownTimerofme=new CountDownTimer(10000,1000) {
    @Override
    public void onTick(long l) {
        timing.setText("time left: "+toString().valueOf(l/1000));
        status.setText("started!!!");
    }

    @Override
    public void onFinish() {
        timing.setText("time left: 0");
        status.setText("completed !!!!");
        workingornot=1;//now able to run again


    }
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timing=(TextView)findViewById(R.id.timer);
    status=(TextView)findViewById(R.id.status);
}
}

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout        xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background=" #8e44ad"
tools:context="com.example.smartbroashad.countdowntimer.MainActivity">

<TextView
    android:id="@+id/status"
    android:layout_width="wrap_content"
    android:layout_height="133dp"
    android:layout_marginBottom="69dp"
    android:layout_marginTop="16dp"
    android:padding="20sp"
    android:text="@string/countdown_timer_not_running"
    android:textAlignment="center"
    android:textAllCaps="true"
    android:textColor="#2c3e50"
    android:textSize="32sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="61dp"
    android:layout_marginTop="32dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/status">


    <EditText
        android:id="@+id/editText7"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="time" />

    <Button
        android:id="@+id/settime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background=" #00aced"
        android:text="set time"
        android:onClick="settimeplease"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="#2c3e50"
        android:textSize="32sp"
        android:textStyle="bold" />


</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="48dp"
    android:layout_marginTop="32dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/linearLayout">

    <TextView
        android:id="@+id/timer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/running"

        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="#2c3e50"
        android:textSize="32sp"
        android:textStyle="bold"
        tools:text="@string/running" />

    <Button
        android:id="@+id/start"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background=" #64D448"
        android:text="start"
        android:onClick="startmeplease"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="#2c3e50"
        android:textSize="32sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background=" #bb0000"
        android:text="stop"
        android:onClick="stopmeplease"
        android:textAlignment="center"
        android:textAllCaps="true"
        android:textColor="#ffffff"
        android:textSize="32sp"
        android:textStyle="bold" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

CountDownTimer timer;
Start.setOnClickListener(new
View.OnClickListener() {
    @Override
    public void onClick(View v) {
        timer=  new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time.setText(String.valueOf(count));
                count++;
            }

            @Override
            public void onFinish() {
                time.setText("Finish");
            }
        };
        timer.start();
    }
});
Stop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        timer.cancel();
    }
});