728x90
이번에는 Android 에서 Java로 스톱워치를 구현해보겠습니다.
전체 코드입니다.
public class MainActivity extends AppCompatActivity {
private Button mStartBtn, mStopBtn, mRecordBtn, mPauseBtn;
private TextView mTimeTextView, mRecordTextView;
private Thread timeThread = null;
private Boolean isRunning = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(Color.parseColor("#4ea1d3"));
}
mStartBtn = (Button) findViewById(R.id.btn_start);
mStopBtn = (Button) findViewById(R.id.btn_stop);
mRecordBtn = (Button) findViewById(R.id.btn_record);
mPauseBtn = (Button) findViewById(R.id.btn_pause);
mTimeTextView = (TextView) findViewById(R.id.timeView);
mRecordTextView = (TextView) findViewById(R.id.recordView);
mStartBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
mStopBtn.setVisibility(View.VISIBLE);
mRecordBtn.setVisibility(View.VISIBLE);
mPauseBtn.setVisibility(View.VISIBLE);
timeThread = new Thread(new timeThread());
timeThread.start();
}
});
mStopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setVisibility(View.GONE);
mRecordBtn.setVisibility(View.GONE);
mStartBtn.setVisibility(View.VISIBLE);
mPauseBtn.setVisibility(View.GONE);
mRecordTextView.setText("");
timeThread.interrupt();
}
});
mRecordBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecordTextView.setText(mRecordTextView.getText() + mTimeTextView.getText().toString() + "\n");
}
});
mPauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isRunning = !isRunning;
if (isRunning) {
mPauseBtn.setText("일시정지");
} else {
mPauseBtn.setText("시작");
}
}
});
}
@SuppressLint("HandlerLeak")
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int mSec = msg.arg1 % 100;
int sec = (msg.arg1 / 100) % 60;
int min = (msg.arg1 / 100) / 60;
int hour = (msg.arg1 / 100) / 360;
@SuppressLint("DefaultLocale") String result = String.format("%02d:%02d:%02d:%02d", hour, min, sec, mSec);
mTimeTextView.setText(result);
}
};
public class timeThread implements Runnable {
@Override
public void run() {
int i = 0;
while (true) {
while (isRunning) { //일시정지를 누르면 멈춤
Message msg = new Message();
msg.arg1 = i++;
handler.sendMessage(msg);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
runOnUiThread(new Runnable(){
@Override
public void run() {
mTimeTextView.setText("");
mTimeTextView.setText("00:00:00:00");
}
});
return;
}
}
}
}
}
}
XML 입니다.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/timeView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="00:00:00:00"
android:textSize="35dp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp">
<Button
android:id="@+id/btn_start"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="시작" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_record"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:layout_marginRight="3dp"
android:text="기록"
android:visibility="invisible" />
<Button
android:id="@+id/btn_pause"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:layout_marginRight="3dp"
android:text="일시정지"
android:visibility="invisible" />
<Button
android:id="@+id/btn_stop"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:text="중지"
android:visibility="invisible" />
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:text="기록"
android:textSize="22dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp">
</View>
<TextView
android:id="@+id/recordView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text=""
android:textSize="22dp" />
</LinearLayout>
</ScrollView>
'안드로이드 자바' 카테고리의 다른 글
[Java][Android] 안드로이드 vector 이미지 취소선 추가 (0) | 2022.03.19 |
---|---|
[Java][Android] 공공데이터 api 사용하기 (0) | 2022.03.17 |
[Java][Android] 안드로이드 CircleImageView 사용해보기 (0) | 2022.03.13 |
[JAVA][Android] notification에 값 담아전달하기 (0) | 2022.03.12 |
[Java][Android] 안드로이드 retrofit DTO/POJO 클래스로 데이터 받아오기 (0) | 2022.03.10 |