1. 程式人生 > >關於安卓如何實現計時器chronometer的暫停和繼續計時功能

關於安卓如何實現計時器chronometer的暫停和繼續計時功能

package com.book.b525;


import java.text.ChoiceFormat;


import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;


public class MainActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer1);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnStop = (Button) findViewById(R.id.btnStop);
Button btnRest = (Button) findViewById(R.id.btnReset);
final Button btnPC=(Button) findViewById(R.id.btnPC);
final EditText edtSetTime = (EditText) findViewById(R.id.setTimeEdit);


btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("--開始記時---");
rangeTime=0;
String ss = edtSetTime.getText().toString();
if (!(ss.equals("") && ss != null)) {
startTime = Integer.parseInt(edtSetTime.getText()
.toString());
}
// 設定開始講時時間

chronometer.setBase(SystemClock.elapsedRealtime());
// 開始記時
chronometer.start();
}
});

//暫停或繼續計時

btnPC.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO 自動生成的方法存根

if(!PAUSE)//暫停計時
{

btnPC.setText("繼續");
//從開始計時計時器執行的時間差。。。。。關鍵
MainActivity.this.rangeTime=SystemClock.elapsedRealtime()-chronometer.getBase();
//Toast.makeText(MainActivity.this, "時間:"+rangeTime, Toast.LENGTH_SHORT).show();
  
   chronometer.stop();
}
else //繼續計時
{
btnPC.setText("暫停");


//設定計時器的基時間。。。。。關鍵
chronometer.setBase(SystemClock.elapsedRealtime()-rangeTime);
   chronometer.start();
  
}


PAUSE=!PAUSE;



}
});




btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 停止
chronometer.stop();
}
});
// 重置
btnRest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
}
});
chronometer
.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
// 如果開始計時到現在超過了startime秒
if (SystemClock.elapsedRealtime()
- chronometer.getBase() > startTime * 1000) {
chronometer.stop();

// 給使用者提示
showDialog();
}
}
});
}


protected void showDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.picture);
builder.setTitle("警告").setMessage("時間到")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}


private int startTime = 0;
private boolean PAUSE=false;
private long rangeTime;//



}