`

Chronometer定时器

    博客分类:
  • Time
 
阅读更多



 

public class ChronometerView extends Activity implements OnClickListener {

	private int startTime = 0;
	Chronometer chronometer;
	EditText edtSetTime;
	Button btnStart;
	Button btnStop;
	Button btnRest;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		initView();
		// SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		// chronometer.setFormat("%s");
		// chronometer.setFormat("计时:%s");
		// 设置开始讲时时间
		chronometer.setBase(SystemClock.elapsedRealtime());
		// 开始记时
		chronometer.start();

		chronometer
				.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
					public void onChronometerTick(Chronometer chronometer) {

						// 如果开始计时到现在超过了startime秒
						// if (SystemClock.elapsedRealtime()
						// - chronometer.getBase() > 1 * 1000) {
						// chronometer.stop();
						// // 给用户提示
						// showDialog();
						// }
					}
				});
	}

	private void initView() {
		chronometer = (Chronometer) findViewById(R.id.chronometer);
		edtSetTime = (EditText) findViewById(R.id.edt_settime);
		btnStart = (Button) findViewById(R.id.btnStart);
		btnStop = (Button) findViewById(R.id.btnStop);
		btnRest = (Button) findViewById(R.id.btnReset);
		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);
		btnRest.setOnClickListener(this);
	}

	protected void showDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setIcon(R.drawable.eb28d25);
		builder.setTitle("警告").setMessage("时间到").setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
					}
				});

		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnStart: {

			System.out.println("--开始记时---");
			String ss = edtSetTime.getText().toString();
			if (!(ss.equals("") && ss != null)) {
				startTime = Integer.parseInt(edtSetTime.getText().toString());
			}
			// 设置开始讲时时间
			chronometer.setBase(SystemClock.elapsedRealtime());
			// 开始记时
			chronometer.start();

		}

			break;
		case R.id.btnStop: {
			// 停止
			chronometer.stop();

		}

			break;
		case R.id.btnReset: {

			chronometer.setBase(SystemClock.elapsedRealtime());

		}

			break;

		default:
			break;
		}
	}
}

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/base1"
    android:orientation="vertical" >

    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:format="%s"
        android:gravity="center"
        android:textColor="#542039"
        android:textSize="50dip" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:gravity="center"
            android:text="设置时间:" />

        <EditText
            android:id="@+id/edt_settime"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="number" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnStart"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="开始记时" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止记时" />

        <Button
            android:id="@+id/btnReset"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="重置" />
    </LinearLayout>

</LinearLayout>

 

  • 大小: 18.8 KB
分享到:
评论
1 楼 lihong11 2012-08-10  
把相应的图片也上传下好不?谢谢啦!

相关推荐

Global site tag (gtag.js) - Google Analytics