-
Notifications
You must be signed in to change notification settings - Fork 12
[김영수_Android] 11주차 과제 제출 #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.view.LayoutInflater | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import android.widget.TextView | ||
| import androidx.recyclerview.widget.RecyclerView | ||
|
|
||
| class LapAdapter : RecyclerView.Adapter<LapAdapter.ViewHolder>() { | ||
|
|
||
| private val lapTimes = mutableListOf<String>() | ||
|
|
||
| inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | ||
| val lapMessage: TextView = itemView.findViewById(R.id.tv_item_message) | ||
| val lapRecord: TextView = itemView.findViewById(R.id.tv_item_record) | ||
| } | ||
|
|
||
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | ||
| val view = LayoutInflater.from(parent.context) | ||
| .inflate(R.layout.item, parent, false) | ||
| return ViewHolder(view) | ||
| } | ||
|
|
||
| override fun onBindViewHolder(holder: ViewHolder, position: Int) { | ||
| holder.lapMessage.text = holder.itemView.context.getString(R.string.tv_message) | ||
| holder.lapRecord.text = lapTimes[position] | ||
| } | ||
|
|
||
| override fun getItemCount(): Int = lapTimes.size | ||
|
|
||
| fun addLap(lap: String) { | ||
| lapTimes.add(0, lap) | ||
| notifyItemInserted(0) | ||
| } | ||
|
|
||
| fun clear() { | ||
| lapTimes.clear() | ||
| notifyDataSetChanged() | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,97 @@ | ||
| package com.example.bcsd_android_2025_1 | ||
|
|
||
| import android.os.Bundle | ||
| import androidx.activity.enableEdgeToEdge | ||
| import android.widget.Button | ||
| import android.widget.TextView | ||
| import androidx.appcompat.app.AppCompatActivity | ||
| import androidx.core.view.ViewCompat | ||
| import androidx.core.view.WindowInsetsCompat | ||
| import androidx.lifecycle.lifecycleScope | ||
| import androidx.recyclerview.widget.LinearLayoutManager | ||
| import androidx.recyclerview.widget.RecyclerView | ||
| import kotlinx.coroutines.Job | ||
| import kotlinx.coroutines.delay | ||
| import kotlinx.coroutines.isActive | ||
| import kotlinx.coroutines.launch | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
|
|
||
| private lateinit var tvTimer : TextView | ||
| private lateinit var btnLap : Button | ||
| private lateinit var btnStart : Button | ||
| private lateinit var btnStop : Button | ||
|
|
||
| private lateinit var rvRecord : RecyclerView | ||
| private lateinit var lapAdapter: LapAdapter | ||
|
|
||
| private var isRunning = false | ||
| private var runningTime = 0L | ||
| private var job: Job? = null | ||
|
|
||
| override fun onCreate(savedInstanceState: Bundle?) { | ||
| super.onCreate(savedInstanceState) | ||
| setContentView(R.layout.activity_main) | ||
|
|
||
| tvTimer = findViewById(R.id.tv_main_timer) | ||
| btnLap = findViewById(R.id.btn_main_lap) | ||
| btnStart = findViewById(R.id.btn_main_start) | ||
| btnStop = findViewById(R.id.btn_main_stop) | ||
| rvRecord = findViewById(R.id.rv_main_record) | ||
| rvRecord.layoutManager = LinearLayoutManager(this) | ||
| lapAdapter = LapAdapter() | ||
| rvRecord.adapter = lapAdapter | ||
|
|
||
| btnStart.setOnClickListener { | ||
| if (isRunning) { | ||
| pauseTimer() | ||
| } else { | ||
| startTimer() | ||
| } | ||
| } | ||
|
|
||
| btnStop.setOnClickListener { | ||
| stopTimer() | ||
| } | ||
|
|
||
| btnLap.setOnClickListener { | ||
| if (isRunning) { | ||
| lapAdapter.addLap(formatTime(runningTime)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private fun startTimer() { | ||
| val startTime = System.currentTimeMillis() - runningTime | ||
| job = lifecycleScope.launch { | ||
| while (isActive) { | ||
| runningTime = System.currentTimeMillis() - startTime | ||
| tvTimer.text = formatTime(runningTime) | ||
| delay(10) | ||
| } | ||
| } | ||
| isRunning = true | ||
| btnStart.text = getString(R.string.btn_pause) | ||
| } | ||
|
|
||
| private fun pauseTimer() { | ||
| job?.cancel() | ||
| isRunning = false | ||
| btnStart.text = getString(R.string.btn_start) | ||
| } | ||
|
|
||
| private fun stopTimer() { | ||
| job?.cancel() | ||
| runningTime = 0L | ||
| isRunning = false | ||
| tvTimer.text = getString(R.string.tv_time) | ||
| btnStart.text = getString(R.string.btn_start) | ||
| lapAdapter.clear() | ||
| } | ||
| } | ||
|
|
||
| private fun formatTime(ms: Long): String { | ||
| val min = (ms/1000)/60 | ||
| val sec = (ms/1000)%60 | ||
| val millisec = (ms%1000) / 10 | ||
| return String.format("%02d:%02d:%02d", min, sec, millisec) | ||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:layout_width="match_parent" | ||
| android:layout_height="wrap_content"> | ||
|
|
||
| <TextView | ||
| android:id="@+id/tv_item_message" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/tv_message" | ||
| android:textSize="15sp"/> | ||
| <TextView | ||
| android:id="@+id/tv_item_record" | ||
| android:layout_width="wrap_content" | ||
| android:layout_height="wrap_content" | ||
| android:text="@string/tv_record" | ||
| android:textSize="15sp" | ||
| android:layout_marginStart="5dp"/> | ||
|
|
||
|
|
||
| </LinearLayout> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,14 @@ | ||
| <resources> | ||
|
|
||
| <!--main--> | ||
| <string name="app_name">BCSD_Android_2025-1</string> | ||
| <string name="tv_time">00:00:00</string> | ||
| <string name="btn_start">Start</string> | ||
| <string name="btn_pause">Pause</string> | ||
| <string name="btn_stop">Stop</string> | ||
| <string name="btn_lap">Lap</string> | ||
| <!--item--> | ||
| <string name="tv_message">경과된 초 :</string> | ||
| <string name="tv_record">00:00:00</string> | ||
|
|
||
| </resources> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
확장함수를 활용하면 좋을 것 같네요