시계 앱의 스톱워치 기능 만들기 코틀린

2022. 12. 6. 18:07_Study/AndroidStudio

728x90

시계 앱의 스톱워치 기능 만들기 코틀린  🐇¸.•*¨*•¸.•*¨*•¸.•*¨*•¸.•*¨*•

해당 자료는 강의 학습자료이며, Do it! 깡샘의 안드로이드 앱 프로그래밍 with 코틀린을 참고하였습니다.


1단계. 새 모듈 생성하기

Ch8_Event라는 이름으로 새로운 모듈

 

2단계. 그래들 설정하기

viewBinding{
    enabled = true
}

 

3단계. 둥근 버튼 만들기

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:padding="10dp">
    <solid android:color="#6666FF"/>
    <corners android:bottomLeftRadius="30dp"
        android:bottomRightRadius="30dp"
        android:topLeftRadius="30dp"
        android:topRightRadius="30dp"/>
</shape>

 

4단계. 앱 화면 구성하기

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Chronometer
        android:id="@+id/chronometer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:gravity="center_horizontal"
        android:textSize="60dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="70dp">
        <Button
            android:id="@+id/startButton"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Start"
            android:background="@drawable/round_button"
            android:textColor="#FFFFFF"
            android:textStyle="bold"/>
        <Button
            android:id="@+id/stopButton"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:background="@drawable/round_button"
            android:textColor="#FFFFFF"
            android:textStyle="bold"
            android:layout_marginLeft="25dp"
            android:enabled="true"/>
        <Button
            android:id="@+id/resetButton"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Reset"
            android:background="@drawable/round_button"
            android:textColor="#FFFFFF"
            android:textStyle="bold"
            android:layout_marginLeft="25dp"
            android:enabled="false"/>
    </LinearLayout>
</RelativeLayout>

 

 

5단계. 메인 액티비티 작성하기

package com.example.ch8_event

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.SystemClock
import android.view.KeyEvent
import android.widget.Toast
import com.example.ch8_event.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    var initTime = 0L // 뒤로 가기 버튼을 누른 시각을 저장하는 속성
    var pauseTime = 0L // 멈춘 시각을 저장하는 속성
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.startButton.setOnClickListener {
            binding.chronometer.base = SystemClock.elapsedRealtime() + pauseTime
            binding.chronometer.start()
// 버튼 표시 여부 조정
            binding.stopButton.isEnabled = true
            binding.resetButton.isEnabled = true
            binding.startButton.isEnabled = false
        }
        binding.stopButton.setOnClickListener {
            pauseTime = binding.chronometer.base - SystemClock.elapsedRealtime()
            binding.chronometer.stop()
            binding.stopButton.isEnabled = false
            binding.resetButton.isEnabled = true
            binding.startButton.isEnabled = true
            binding.resetButton.setOnClickListener {
                pauseTime = 0L
                binding.chronometer.base = SystemClock.elapsedRealtime()
                binding.chronometer.stop()
                binding.stopButton.isEnabled = false
                binding.resetButton.isEnabled = false
                binding.startButton.isEnabled = true
            }
        }
    }// onCreate
        // 뒤로 가기 버튼 이벤트 핸들러
        override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if(System.currentTimeMillis() - initTime > 3000) {
                    Toast.makeText(this, "종료하려면 한 번 더 누르세요!!", Toast.LENGTH_SHORT).show()
                    initTime = System.currentTimeMillis()
                    return true
                }
            }
            return super.onKeyDown(keyCode, event)
        }
} // class MainActivity

 

 

 

6단계. 앱 실행하기