1安卓点击按钮定时器启动显示和停止的例子
未经允许,请勿转载
下载链接:http://download.csdn.net/download/chencaw/10256340
2资源代码
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.chentimer.MainActivity"> <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="44dp" android:layout_marginTop="32dp" android:text="启动" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <EditText android:id="@+id/editText" android:layout_width="134dp" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:ems="10" android:inputType="textPersonName" android:text="0" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" tools:layout_editor_absoluteY="112dp" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp" android:layout_marginStart="8dp" android:layout_marginTop="32dp" android:text="停止" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/button_start" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout>
3 程序代码
package com.example.lenovo.chentimer; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { private static String TAG = "TimerDemo"; private Button btstart = null; private Button btstop = null; private TextView texttime = null; // Handler handler = new Handler(); // private boolean chenflag = false; // Runnable runnable =null; private Timer mTimer = null; private TimerTask mTimerTask = null; private Handler mHandler = null; private static int count = 0; private boolean isPause = false; private boolean isStop = true; private static int delay = 1000; //1s private static int period = 1000; //1s private static final int UPDATE_TEXTVIEW = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btstart = (Button) findViewById(R.id.button_start); btstop = (Button) findViewById(R.id.button_stop); texttime = (TextView) findViewById(R.id.editText); btstart.setOnClickListener(listener); btstop.setOnClickListener(listener); btstop.setEnabled(false); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case UPDATE_TEXTVIEW: updateTextView(); break; default: break; } } }; } private View.OnClickListener listener = new View.OnClickListener() { public void onClick(View v) { if (v == btstart) { startTimer(); btstart.setEnabled(false); btstop.setEnabled(true); } if (v == btstop) { stopTimer(); btstart.setEnabled(true); btstop.setEnabled(false); } } }; private void startTimer(){ if (mTimer == null) { mTimer = new Timer(); } if (mTimerTask == null) { mTimerTask = new TimerTask() { @Override public void run() { Log.i(TAG, "count: "+String.valueOf(count)); sendMessage(UPDATE_TEXTVIEW); do { try { Log.i(TAG, "sleep(1000)..."); Thread.sleep(1000); } catch (InterruptedException e) { } } while (isPause); count ++; } }; } if(mTimer != null && mTimerTask != null ) mTimer.schedule(mTimerTask, delay, period); } private void stopTimer(){ if (mTimer != null) { mTimer.cancel(); mTimer = null; } if (mTimerTask != null) { mTimerTask.cancel(); mTimerTask = null; } count = 0; } public void sendMessage(int id){ if (mHandler != null) { Message message = Message.obtain(mHandler, id); mHandler.sendMessage(message); } } private void updateTextView(){ texttime.setText(String.valueOf(count)); } }