0

我只想将时间设置为 10 分钟。每当完成/完成 10 分钟时,我的按钮立即被禁用并弹出消息。我已经在按钮事件上启动计时器并在按钮事件上停止。但是我想开始我的新活动开始的时间,并且我的所有按钮和文本视图在空闲/自动完成 10 分钟时被禁用。并显示弹出消息。这该怎么做?

这是我的代码。

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>

活动代码

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

        }

    };

}
4

4 回答 4

8

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

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();
于 2013-11-13T05:43:30.840 回答
2

在活动的 onCreate() 中创建一个 Timer。然后禁用按钮并使用达到计时器周期时执行的 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);

}
于 2013-11-13T06:04:43.897 回答
1

您应该只能使用计时器:

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() 方法中。

于 2013-11-13T05:42:14.280 回答
0

试试这个代码。它应该适合你

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

只需startTime()在您的 startButtononClick方法中调用该方法并暂停 call pauseTime()。如果您想恢复,请startTime()再次致电

于 2013-11-13T05:42:24.990 回答