한 걸음 두 걸음
Android Timer Stopwatch 만들기~ 소스코드 예제 본문
반응형
예시 1 : chronometer사용
start 버튼 누르면 pause버튼으로 바뀌며, start시 m:s로 시간이 표시된다.
activity_timer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Watch"
android:textSize="20dp"
android:gravity="center"
android:layout_marginBottom="10dp"/>
<Chronometer
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="@+id/chronometer"
android:layout_marginBottom="10dp"/>
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Start" />
<Button
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Pause"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:id="@+id/resetBtn"
android:layout_marginBottom="10dp"/>
</LinearLayout>
MainActivity.java
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class /\_이름주의\_/TimerActivity extends AppCompatActivity {
Chronometer chronometer;
Button startBtn, pauseBtn, resetBtn;
long stopTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);/*이름주의*/
chronometer = (Chronometer)findViewById(R.id.chronometer);
startBtn = (Button)findViewById(R.id.startBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
resetBtn = (Button)findViewById(R.id.resetBtn);
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime() + stopTime);
chronometer.start();
startBtn.setVisibility(View.GONE);
pauseBtn.setVisibility(View.VISIBLE);
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopTime = chronometer.getBase() - SystemClock.elapsedRealtime();
chronometer.stop();
startBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.GONE);
}
});
resetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chronometer.setBase(SystemClock.elapsedRealtime());
stopTime = 0;
chronometer.stop();
startBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.GONE);
}
});
}
}
예시 2 : 스레드사용
Runnable에서 SystemClock 내의 uptimeMillis()함수를 사용했다.
static long elapsedRealtime()
딥슬립 시간을 포함한 부팅 후 경과한 시간을 밀리초 단위로 리턴함.
static long uptimeMillis()
딥슬립 시간을 제외한 부팅 후 경과한 시간을 밀리초 단위로 리턴함.
참고URL : http://don-forget.blogspot.com/2012/05/blog-post.html
activity_timer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ECEFF1">
<TextView
android:text="00:00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="50dp"
android:textStyle="bold"
android:textColor="#009688"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:id="@+id/button" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/button3" />
<Button
android:text="Save Lap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button4"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="@+id/listview1"/>
</RelativeLayout>
TimerActivity.java
package com.gkskfhdlstmapk.hanpinetree.bcsd_navi_listview_calc;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TimerActivity extends AppCompatActivity {
TextView textView ;
Button start, pause, reset, lap ;
long MillisecondTime, StartTime, TimeBuff, UpdateTime = 0L ;
Handler handler;
int Seconds, Minutes, MilliSeconds ;
ListView listView ;
String[] ListElements = new String[] { };
List<String> ListElementsArrayList ;
ArrayAdapter<String> adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
textView = (TextView)findViewById(R.id.textView);
start = (Button)findViewById(R.id.button);
pause = (Button)findViewById(R.id.button2);
reset = (Button)findViewById(R.id.button3);
lap = (Button)findViewById(R.id.button4) ;
listView = (ListView)findViewById(R.id.listview1);
handler = new Handler() ;
ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
ListElementsArrayList
);
listView.setAdapter(adapter);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
StartTime = SystemClock.uptimeMillis();
handler.postDelayed(runnable, 0);
reset.setEnabled(false);
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TimeBuff += MillisecondTime;
handler.removeCallbacks(runnable);
reset.setEnabled(true);
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MillisecondTime = 0L ;
StartTime = 0L ;
TimeBuff = 0L ;
UpdateTime = 0L ;
Seconds = 0 ;
Minutes = 0 ;
MilliSeconds = 0 ;
textView.setText("00:00:00");
ListElementsArrayList.clear();
adapter.notifyDataSetChanged();
}
});
lap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ListElementsArrayList.add(textView.getText().toString());
adapter.notifyDataSetChanged();
}
});
}
public Runnable runnable = new Runnable() {
public void run() {
MillisecondTime = SystemClock.uptimeMillis() - StartTime;
UpdateTime = TimeBuff + MillisecondTime;
Seconds = (int) (UpdateTime / 1000);
Minutes = Seconds / 60;
Seconds = Seconds % 60;
MilliSeconds = (int) (UpdateTime % 1000);
textView.setText("" + Minutes + ":"
+ String.format("%02d", Seconds) + ":"
+ String.format("%03d", MilliSeconds));
handler.postDelayed(this, 0);
}
};
}
반응형
'FrontEnd > Android' 카테고리의 다른 글
android ActionBar에 Icon 붙이기 (0) | 2019.04.08 |
---|---|
android webview 웹뷰 사용하기 google / local web / (0) | 2019.04.08 |
ListView / GridView / RecyclerView 란? (0) | 2019.03.29 |
android drawerLayout / NavigaionDrawer활용하기 (0) | 2019.03.22 |
BCSD Fragment-Notification과제 깔끔하게 만들자 (0) | 2019.03.22 |