Android Application
Kotlin 고급 위젯 - 날짜와 시간 관련 위젯 : 크로노미터(Chronometer)
작성자 임베디드코리아
작성일 23-09-24 00:03
조회2,013회
댓글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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Chronometer
android:id="@+id/exChronometer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:format="시간 측정 : %s"
android:gravity="center"
android:textSize="30dp" />
<Button
android:id="@+id/startBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="START"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
<Button
android:id="@+id/stopBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="STOP"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
<Button
android:id="@+id/restartBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESTART"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
<Button
android:id="@+id/setBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SET FORMAT"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
<Button
android:id="@+id/clearBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR FORMAT"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
------------------------------------------------------------------------------------
MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
private lateinit var simpleChronometer: Chronometer
private lateinit var start: Button
private lateinit var stop: Button
private lateinit var restart: Button
private lateinit var setFormat: Button
private lateinit var clearFormat: Button
var pauseTime=0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
simpleChronometer = findViewById(R.id.exChronometer)
start = findViewById(R.id.startBT)
stop = findViewById(R.id.stopBT)
restart= findViewById(R.id.restartBT)
setFormat = findViewById(R.id.setBT)
clearFormat = findViewById(R.id.clearBT)
start.setOnClickListener {
simpleChronometer.start()
}
stop.setOnClickListener {
pauseTime = simpleChronometer.base - SystemClock.elapsedRealtime()
simpleChronometer.stop()
}
restart.setOnClickListener {
simpleChronometer.base = SystemClock.elapsedRealtime()+pauseTime
simpleChronometer.start()
}
setFormat.setOnClickListener {
simpleChronometer.setFormat("새로운 형식 : %s")
}
clearFormat.setOnClickListener {
simpleChronometer.setFormat("")
}
}
}
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.