본문 바로가기
Android Studio

안드로이드 #8 : chronometer 스탑워치 앱 230306

by haheehee 2023. 3. 6.
package com.example.ch8_event

import android.os.Bundle
import android.os.SystemClock
import androidx.appcompat.app.AppCompatActivity
import com.example.ch8_event.databinding.ActivityMain7Binding

class MainActivity7 : AppCompatActivity() {
    // 뒤로가기 버튼
    var initTime = 0L
    // 멈춘시간 저장
    var pauseTime = 0L

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMain7Binding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.btnStart.setOnClickListener {
            binding.chronometer.base = SystemClock.elapsedRealtime() + pauseTime
            binding.chronometer.start()
            // 버튼 표시
            binding.btnStop.isEnabled = true
            binding.btnReset.isEnabled = true
            binding.btnStart.isEnabled = false
            binding.btnRestart.isEnabled = true
        }
        binding.btnStop.setOnClickListener {
            pauseTime = binding.chronometer.base - SystemClock.elapsedRealtime()
            binding.chronometer.stop()
            // 버튼 표시
            binding.btnStop.isEnabled = false
            binding.btnReset.isEnabled = true
            binding.btnStart.isEnabled = true
            binding.btnRestart.isEnabled = true
        }
        binding.btnReset.setOnClickListener {
            pauseTime = 0L
            binding.chronometer.base = SystemClock.elapsedRealtime()
            binding.chronometer.stop()
            // 버튼 표시
            binding.btnStop.isEnabled = false
            binding.btnReset.isEnabled = false
            binding.btnStart.isEnabled = true
            binding.btnRestart.isEnabled = false
        }
        binding.btnRestart.setOnClickListener {
            pauseTime = 0L
            binding.chronometer.base = SystemClock.elapsedRealtime()
            binding.chronometer.start()
            // 버튼 표시
            binding.btnStop.isEnabled = true
            binding.btnReset.isEnabled = true
            binding.btnStart.isEnabled = false
            binding.btnRestart.isEnabled = true
        }
    }
}

MainActivity7

 

<?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="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text=""
        android:textSize="40dp"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="100dp">

        <Button
            android:id="@+id/btnStart"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@drawable/roundbutton"
            android:text="Start"
            android:textColor="#FFFFFF"
            android:textSize="10dp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btnStop"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@drawable/roundbutton"
            android:text="Stop"
            android:textColor="#FFFFFF"
            android:textSize="10dp"
            android:textStyle="bold"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:enabled="false"/>

        <Button
            android:id="@+id/btnReset"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@drawable/roundbutton"
            android:text="Reset"
            android:textColor="#FFFFFF"
            android:textSize="10dp"
            android:textStyle="bold"
            android:layout_marginRight="5dp"
            android:enabled="false" />

        <Button
            android:id="@+id/btnRestart"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:background="@drawable/roundbutton"
            android:text="Restart"
            android:textColor="#FFFFFF"
            android:textSize="10dp"
            android:textStyle="bold"
            android:enabled="false" />
    </LinearLayout>
</RelativeLayout>

activity_main7.xml

 

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

@drawable폴더에 roundbutton.xml 파일도 만들어주었다~

 

역시나 manifest파일도 확인 후, 해당 파일로 바꿔주고

 

- 결과 -

 

 

댓글