پروژه ساعت ساده مکتب خونه بخش دوم- نمایش خودکار تاریخ- هندلر-کرنومتر-کرونومتر
سه شنبه, ۹ ارديبهشت ۱۳۹۹، ۰۴:۴۵ ب.ظ
SimpleDateFormat df; TextView elapsed; Button startbt, pausebt; Long time=0L,starttime; Handler hn; df = new SimpleDateFormat("HH:mm:ss:SSS"); df.setTimeZone(TimeZone.getTimeZone("UTC")); hn=new Handler(); private Runnable runnable =new Runnable() { @Override public void run() { time=SystemClock.uptimeMillis()-starttime; elapsed.setText(df.format(time)); hn.postDelayed(this,0); } }; elapsed.setText(df.format(0L)); startbt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { starttime = SystemClock.uptimeMillis(); hn.postDelayed(runnable, 0); } }); pausebt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hn.removeCallbacks(runnable); } }); @Override public void onPause() { super.onPause(); hn.removeCallbacks(runnable); }
package ir.maktabkhune.android.simpleclock; import android.app.Fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.ViewGroup; import android.os.Handler; import android.os.SystemClock; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.text.SimpleDateFormat; import java.util.TimeZone; public class fragstopw extends Fragment { SimpleDateFormat df; TextView elapsed; Button startbt, pausebt; Long time=0L,starttime; Handler hn; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragmentstopwatch, container, false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); config(view); onclicks(view); pausebt.setEnabled(false); elapsed.setText(df.format(0L)); } private void onclicks(View view) { startbt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { starttime = SystemClock.uptimeMillis(); hn.postDelayed(runnable, 0); pausebt.setEnabled(true); startbt.setEnabled(false); } }); pausebt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hn.removeCallbacks(runnable); pausebt.setEnabled(false); startbt.setEnabled(true); } }); } private void config(View v) { df = new SimpleDateFormat("HH:mm:ss:SSS"); df.setTimeZone(TimeZone.getTimeZone("UTC")); startbt=v.findViewById(R.id.stop_watch_start_btn); pausebt=v.findViewById(R.id.stop_watch_stop_btn); elapsed=v.findViewById(R.id.stop_watch_time_passed); hn=new Handler(); } private Runnable runnable =new Runnable() { @Override public void run() { time=SystemClock.uptimeMillis()-starttime; elapsed.setText(df.format(time)); hn.postDelayed(this,0); } }; @Override public void onPause() { super.onPause(); hn.removeCallbacks(runnable); pausebt.setEnabled(false); startbt.setEnabled(true); } }
۹۹/۰۲/۰۹