Java 在安卓系统中,如何设置只有计时器的时间为10分钟?
我只想将时间设置为10分钟。每当完成/完成10分钟时,我的按钮立即被禁用并弹出消息。我已在按钮事件上启动计时器,并在按钮事件上停止。但是我想在我的新活动开始时开始,我的所有按钮和文本视图在自由/完成10分钟时自动禁用。并显示弹出消息。如何做到这一点 这是我的密码 xml文件Java 在安卓系统中,如何设置只有计时器的时间为10分钟?,java,android,Java,Android,我只想将时间设置为10分钟。每当完成/完成10分钟时,我的按钮立即被禁用并弹出消息。我已在按钮事件上启动计时器,并在按钮事件上停止。但是我想在我的新活动开始时开始,我的所有按钮和文本视图在自由/完成10分钟时自动禁用。并显示弹出消息。如何做到这一点 这是我的密码 xml文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/re
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000" >
<TextView
android:id="@+id/timerValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pauseButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="37dp"
android:text="@string/timerVal"
android:textColor="#ffffff"
android:textSize="40sp" />
<Button
android:id="@+id/startButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="38dp"
android:text="@string/startButtonLabel" />
<Button
android:id="@+id/pauseButton"
android:layout_width="90dp"
android:layout_height="45dp"
android:layout_alignBaseline="@+id/startButton"
android:layout_alignBottom="@+id/startButton"
android:layout_alignParentRight="true"
android:layout_marginRight="38dp"
android:text="@string/pauseButtonLabel" />
</RelativeLayout>
您应该能够使用计时器:
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask(){
@Override
public void run(){
Button startButton = findViewById(R.id.startButton);
startButton.setEnabled(false)
Button pauseButton = findViewById(R.id.pauseButton);
pauseButton.setEnabled(false)
//And handle AlertView here
}
}, 60 * 10 * 1000)
然后在onCreate()方法中设置它。试试下面的代码。它应该对你有用
//YOUR GLOBAL VARIABLES
private static final long UPDATE_INTERVAL = 1000;
private static final long DELAY_INTERVAL = 0;
int seconds = 0;
Timer timer;
private void startTime() {
timer = new Timer();
timer.scheduleAtFixedRate(
new TimerTask() {
public void run() {
seconds++;
checkTime();
}
},
DELAY_INTERVAL,
UPDATE_INTERVAL);
}
private void checkTime() {
int day = (int)TimeUnit.SECONDS.toDays(seconds);
long hours = TimeUnit.SECONDS.toHours(seconds) - (day *24);
long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
if (minute == 10) {
timer.cancel();
}
}
private void pauseTime() {
if (timer != null)
timer.cancel();
}
只需在startButton
onClick
方法中调用startTime()
方法,并暂停调用pauseTime()
。如果要继续,只需再次调用startTime()
使用倒计时来实现这一点
new CountDownTimer(60**10*1000, 1000) {
public void onTick(long millisUntilFinished) {
time.setText("seconds remaining: "
+ millisUntilFinished / 1000);
}
public void onFinish() {
here you write the function to start
button to disable and popup
}
}.start();
在活动的onCreate()中创建计时器。然后禁用按钮,并使用达到计时器周期时执行的Runnable来制作土司
private Button button1;
private Button button2;
private TextView textview;
@overide
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_one);
button1 = (Button) findViewById(R.id.startButton);
button2 = (Button) findViewById(R.id.pauseButton);
textView = (TextView) findViewById(R.id.timerValue);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
button1.setEnabled(false);
button2.setEnabled(false);
textView.setEnabled(false);
}
}, 0, 10 * 60 * 1000);
}
这是一个很好的方法,但他有一个暂停的方法。你只能取消倒计时而不能暂停它,这样当你再次启动它时,它会从一开始就重新启动。你太棒了。我的代码工作正常。谢谢你的帮助。
private Button button1;
private Button button2;
private TextView textview;
@overide
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_one);
button1 = (Button) findViewById(R.id.startButton);
button2 = (Button) findViewById(R.id.pauseButton);
textView = (TextView) findViewById(R.id.timerValue);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
button1.setEnabled(false);
button2.setEnabled(false);
textView.setEnabled(false);
}
}, 0, 10 * 60 * 1000);
}