eclipse 如何通过按钮启动和停止倒数计时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4788086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I start and stop my countdowntimer via a button?
提问by MJ93
This is what I have so far.. it just starts when the application is opened:
这就是我到目前为止所拥有的..它在应用程序打开时才开始:
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();
}
}
I know that I need to call start()
inside a button procedure. However, if I move the .start()
from where it's at the new CountDownTimer(100000 , 1000) {
gets an error.
我知道我需要start()
在按钮过程中调用。但是,如果我.start()
从它所在的位置移动它,new CountDownTimer(100000 , 1000) {
则会出现错误。
回答by Cristian
Well... maybe you need to first understand how Java and programming work. Then, you can try to do something like this:
嗯……也许您需要先了解 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();
回答by Ashad Nasim
You can do it like this to make a thing what you want. Here is the Java code:
你可以像这样做一个你想要的东西。这是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);
}
}
Here is the XML:
这是 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>
回答by M.Chithirai Perumal
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();
}
});