英文:
How to add a listener event to a seek bar in android studio using kotlin?
问题
I tried this but getting error because the function myListener should return something.
我尝试了这个,但出现了错误,因为函数myListener应该返回某些内容。
What I want to do is to get the seek bar value each time I'm changing its value at runtime and assign the value to another variable named counter.
我想要做的是,在运行时每次更改滑动条的值时,获取其值并将该值分配给另一个名为counter的变量。
class MainActivity : AppCompatActivity() {
val seek = findViewById<SeekBar>(R.id.seekBar)
var flashLightStatus: Boolean = false
var counter = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
seek.setOnSeekBarChangeListener(myListener())
startFlashLight()
}
private fun myListener(): SeekBar.OnSeekBarChangeListener {
return object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
// Update the counter variable with the seek bar's current value.
counter = progress
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
// Do nothing here if needed.
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// Do nothing here if needed.
}
}
}
}
Here's the updated code that assigns the seek bar value to the counter
variable when it changes during runtime.
英文:
I tried this but getting error because the function myListener should return something.
what I want to do is to get the seek bar value each time I'm changing it's value at runtime and assign the value to another variable name counter.
class MainActivity : AppCompatActivity() {
val seek = findViewById<SeekBar>(R.id.seekBar)
var flashLightStatus: Boolean = false
val counter = 0;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
seek.setOnSeekBarChangeListener(myListener())
startFlashLight()
}
private fun myListener(): SeekBar.OnSeekBarChangeListener? {
}
答案1
得分: 1
你需要扩展函数 `onProgressChanged`
```kotlin
seek.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
// Log.d("seekbar", "Your Progress: ${seekBar?.progress}")
}
override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
},
)
英文:
You have to extend the function onProgressChanged
seek.setOnSeekBarChangeListener(
object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
// Log.d("seekbar", "Your Progress: ${seekBar?.progress}"
}
override fun onStartTrackingTouch(seekBar: SeekBar?) = Unit
override fun onStopTrackingTouch(seekBar: SeekBar?) = Unit
},
)
答案2
得分: 0
你可以使用这段代码并在运行时监听数值变化。
英文:
you can use this code and listen value change runtime
seekBarView.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onStopTrackingTouch(seekBar: SeekBar) {
Log.i(TAG, "onStopTrackingTouch: ")
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onProgressChanged(
seekBar: SeekBar, progress: Int,
fromUser: Boolean
)
// here
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论