<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SeekBar
android:id="@+id/sb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="0"/>
<SeekBar
android:id="@+id/sb2"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="3" />
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="값 가져오기"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 증가하기"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 감소하기"/>
</LinearLayout>
package com.mobile.seekbartest
import android.os.Bundle
import android.widget.SeekBar
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1.setOnClickListener { view ->
tv1.text = "seek1 : ${sb1.progress}"
tv2.text = "seek2 : ${sb2.progress}"
}
btn2.setOnClickListener { view ->
sb1.incrementProgressBy(1)
sb2.incrementProgressBy(1)
}
btn3.setOnClickListener { view ->
sb1.incrementProgressBy(-1)
sb2.incrementProgressBy(-1)
}
var listener = SeekBarListener()
sb1.setOnSeekBarChangeListener(listener)
/* 중요!! */
sb2.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
tv2.text = "seek2 : $p1"
}
override fun onStartTrackingTouch(p0: SeekBar?) {
}
override fun onStopTrackingTouch(p0: SeekBar?) {
}
})
}
inner class SeekBarListener : SeekBar.OnSeekBarChangeListener{
// SeekBar의 값이 변경되었을 때!
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) { // p1 : 설정된 값이 들어온다. p2 : 터치해서 값을 바꾸면 true 코드를 통해서 바꾸면 false
tv1.text = "seek1 : $p1"
}
// 값을 변경하기 위해 유저가 터치했을 때
override fun onStartTrackingTouch(p0: SeekBar?) {
}
// 값을 변경한 후 터치를 때었을 때
override fun onStopTrackingTouch(p0: SeekBar?) {
}
}
}
incrementProgressBy()를 통해서 값을 증가시키고 감소시킬 수 있다. Progress Bar와는 다르게 리스너가 존재한다. SeekBar.OnSeekBarChangeListener를 상속시키고 메서드를 오버라이드 할때 3가지 메서드가 오버라이딩 되는데,
첫 번째 onProgressChanged() 는 값이 변경되었을 때 구현하고 싶은 코드를 넣으면된다.
두 번째 onStartTrackingTouch() 는 값을 변경하기 위해 유저가 터치했을 때,
세 번째 onStopTrackingTouch() 는 값을 변경한 후 터치를 때었을 때 사용한다.
그리고 이 리스너는 오버라이딩 할 메서드가 3개나 되기 때문에 기본 람다식을 사용할 수 없다. 그렇기 때문에 object에 리스너를 implement 시켜서 오버라이딩 한다!
궁금하신 점은 덧글로 적어주세요.
'안드로이드' 카테고리의 다른 글
[Android Kotlin] 안드로이드 코틀린 ImageView (0) | 2020.03.21 |
---|---|
[Android Kotlin] 안드로이드 코틀린 EditText (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Progress Bar (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Radio Button (0) | 2020.03.21 |
[Android Kotlin] 안드로이드 코틀린 Check Box (0) | 2020.03.21 |