Android Application

Kotlin 스레드(Thread) : SeekBar를 이용한 Thread

작성자 임베디드코리아 작성일23-10-23 23:28 조회1,544회 댓글0건

첨부파일

Kotlin 스레드(Thread) : SeekBar를 이용한 Thread

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="1번 진행률 :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <SeekBar
        android:id="@+id/pb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="10" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="2번 진행률 :"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <SeekBar
        android:id="@+id/pb2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="스레드 시작" />
</LinearLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    lateinit var pb1: SeekBar
    lateinit var pb2: SeekBar
    lateinit var btn: Button
    lateinit var tv1: TextView
    lateinit var tv2: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        pb1 = findViewById(R.id.pb1)
        pb2 = findViewById(R.id.pb2)
        btn = findViewById(R.id.button1)

        tv1 = findViewById(R.id.tv1)
        tv2 = findViewById(R.id.tv2)

        btn.setOnClickListener {
            object : Thread() {
                override fun run() {
                    for(i in pb1.progress .. 99 ) {
                        pb1.progress = pb1.progress + 2
                        tv1.text= "1번 진행률 : " + pb1.progress.toString() + "%"
                        SystemClock.sleep(200)
                    }
                }
            }.start()

            object : Thread() {
                override fun run() {
                    for (i in pb2.progress..99) {
                        pb2.progress = pb2.progress + 1
                        tv2.text= "2번 진행률 : " + pb2.progress.toString() + "%"
                        SystemClock.sleep(200)
                    }
                }
            }.start()
        }

    }
}

------------------------------------------------------------------------------------
    themes.xml
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.ThreadSeekBar" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">#00BCD4</item>
        <item name="colorPrimaryDark">#004289</item>
    </style>

    <style name="Theme.ThreadSeekBar" parent="Base.Theme.ThreadSeekBar" />
</resources>