<?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:gravity="center" android:orientation="vertical" > <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> <Chronometer android:id="@+id/chronometer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#ff0000" android:textSize="60dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dip" android:orientation="horizontal" > <Button android:id="@+id/btntimeopp" 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>
package com.example.yanlei.yl; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Chronometer; import android.widget.EditText; public class MainActivity extends AppCompatActivity { // 设置初始时间 private int startTime = 0; // 声明计时器Chronomete对象 private Chronometer chronometer; // 定义计时器操作按钮 private Button btnOperate; // 定义计时器重置按钮 private Button btnRest; // 定义到达时间的edittext private EditText edtSetTime; // 开始boolOperate=true,停止boolOperate=false private Boolean boolOperate = true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 得到布局中的所有对象 findView(); // 设置对象的监听器 setListener(); } // 得到布局中的所有对象 private void findView() { // TODO Auto-generated method stub chronometer = (Chronometer) findViewById(R.id.chronometer); btnOperate = (Button) findViewById(R.id.btntimeopp); btnRest = (Button) findViewById(R.id.btnReset); edtSetTime = (EditText) findViewById(R.id.edt_settime); } // 得到布局中的所有对象 private void setListener() { // TODO Auto-generated method stub btnRest.setOnClickListener(listener); btnOperate.setOnClickListener(listener); chronometer .setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { @Override public void onChronometerTick(Chronometer chronometer) { // 如果开始计时到现在超过了startime秒 if (SystemClock.elapsedRealtime() - chronometer.getBase() > startTime * 1000) { // 计时器停止 chronometer.stop(); // 给用户提示 showDialog(); } } }); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { // 开始按钮 case R.id.btntimeopp: // 如果boolOperate==true,说明计时开始 if (boolOperate) { String ss = edtSetTime.getText().toString(); // 把到达时间转化为Int类型 if (!(ss.equals("") && ss != null)) { startTime = Integer.parseInt(edtSetTime.getText() .toString()); } // 设置开始时间 chronometer.setBase(SystemClock.elapsedRealtime()); // 开始记时 chronometer.start(); boolOperate = false; // eidttext不可点 edtSetTime.setClickable(false); btnOperate.setText("点击停止"); } // 如果boolOperate==false,说明计时结束 else { // 计时停止 chronometer.stop(); boolOperate = true; // eidttex可点 edtSetTime.setClickable(true); btnOperate.setText("点击开始"); } break; // 重置按钮 case R.id.btnReset: chronometer.setBase(SystemClock.elapsedRealtime()); break; default: break; } } }; protected void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("闫磊温馨提醒你").setMessage("设置:"+startTime+"秒,时间到了") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); AlertDialog dialog = builder.create(); dialog.show(); } }