gpt4 book ai didi

java - 如何在 Android 中仅使用计时器设置 10 分钟?

转载 作者:行者123 更新时间:2023-12-01 06:39:05 25 4
gpt4 key购买 nike

我只想将时间设置为 10 分钟。每当完成/完成 10 分钟时,我的按钮就会被禁用并弹出消息。我已在按钮事件上启动计时器并在按钮事件上停止。但我想在我的新 Activity 启动时开始计时,并且在空闲/完成 10 分钟时自动禁用我的所有按钮和 TextView 。并显示弹出消息。如何做到这一点?

这是我的代码。

xml文件

<?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>

Activity 代码

public class TimerDemo extends Activity
{

private Button startButton;
private Button pauseButton;

private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();

long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.demo_one);

timerValue = (TextView) findViewById(R.id.timerValue);

startButton = (Button) findViewById(R.id.startButton);

startButton.setOnClickListener(new View.OnClickListener()
{

public void onClick(View view)
{

startTime = SystemClock.uptimeMillis();

customHandler.postDelayed(updateTimerThread, 0);

}

});

pauseButton = (Button) findViewById(R.id.pauseButton);

pauseButton.setOnClickListener(new View.OnClickListener()
{

public void onClick(View view)
{

timeSwapBuff += timeInMilliseconds;

customHandler.removeCallbacks(updateTimerThread);

}

});

}

private Runnable updateTimerThread = new Runnable()
{

public void run()
{

timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

updatedTime = timeSwapBuff + timeInMilliseconds;

int secs = (int) (updatedTime / 1000);

int mins = secs / 60;

secs = secs % 60;

int milliseconds = (int) (updatedTime % 1000);

timerValue.setText("" + mins + ":"

+ String.format("%02d", secs) + ":"

+ String.format("%03d", milliseconds));

customHandler.postDelayed(this, 0);

}

};

}

最佳答案

使用倒计时器来实现这一点

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

关于java - 如何在 Android 中仅使用计时器设置 10 分钟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com