Android Application
Kotlin 고급 위젯 - 크로노미터(Chronometer): 스톱워치 구현하기
작성자 임베디드코리아
작성일 23-09-24 12:59
조회1,680회
댓글0건
------------------------------------------------------------------------------------
activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<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" />
<ImageView
android:id="@+id/runImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/run"
android:layout_below="@+id/chronometer"
android:layout_marginTop="80dp"
android:visibility="invisible"/>
<ImageView
android:id="@+id/stopImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/stop"
android:layout_below="@+id/chronometer"
android:layout_marginTop="80dp"
android:visibility="invisible"/>
<ImageView
android:id="@+id/watchImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:src="@drawable/watch"
android:layout_below="@+id/chronometer"
android:layout_marginTop="80dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="70dp">
<Button
android:id="@+id/startButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#00897B"
android:text="START"
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="#00897B"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_marginLeft="25dp"
android:enabled="false"/>
<Button
android:id="@+id/resetButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="RESET"
android:background="#00897B"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:layout_marginLeft="25dp"
android:enabled="false"/>
</LinearLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
var initTime = 0L // 뒤로가기 버튼을 누른 시각을 저장하는 속성
var pauseTime = 0L // 멈춘 시간을 저장하는 속성
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.startButton.setOnClickListener {
binding.chronometer.base = SystemClock.elapsedRealtime() + pauseTime // 이전 시간부터 스타트
binding.chronometer.start()
binding.startButton.isEnabled = false
binding.stopButton.isEnabled = true
binding.resetButton.isEnabled = true
binding.runImage.isVisible = true
binding.stopImage.isVisible = false
binding.watchImage.isVisible = false
}
binding.stopButton.setOnClickListener {
pauseTime = binding.chronometer.base - SystemClock.elapsedRealtime()
binding.chronometer.stop()
binding.startButton.isEnabled = true
binding.stopButton.isEnabled = false
binding.resetButton.isEnabled = true
binding.runImage.isVisible = false
binding.stopImage.isVisible = true
binding.watchImage.isVisible = false
}
binding.resetButton.setOnClickListener {
pauseTime = 0L
binding.chronometer.base = SystemClock.elapsedRealtime() // ??
binding.chronometer.stop()
binding.startButton.isEnabled = true
binding.stopButton.isEnabled = false
binding.resetButton.isEnabled = false
binding.runImage.isVisible = false
binding.stopImage.isVisible = false
binding.watchImage.isVisible = true
}
}
// 뒤로가기 버튼 이벤트 핸들러
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// 뒤로가기 버튼을 눌렀을 때
if (keyCode == KeyEvent.KEYCODE_BACK) {
// 뒤로가기 버튼을 처음 눌렀거나 누른 지 3초가 지났을 때 처리 (3초내 한 번 더 누를 시 종료)
if (System.currentTimeMillis() - initTime > 3000) {
Toast.makeText(this, "종료하려면 한 번 더 누르세요!!", Toast.LENGTH_SHORT).show()
initTime = System.currentTimeMillis()
return true
}
}
return super.onKeyDown(keyCode, event)
}
}
------------------------------------------------------------------------------------
build.gradle(Module)
-------------------------------------------------------------------------------------
// 안드로이드 스튜디오 4.0 이상
android {
...
buildFeatures {
viewBinding = true
}
}
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.