如何在Android Studio中使用Kotlin为SeekBar添加监听器事件?

huangapple go评论66阅读模式
英文:

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&lt;SeekBar&gt;(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(&quot;seekbar&quot;, &quot;Your Progress: ${seekBar?.progress}&quot;	
		}

		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, &quot;onStopTrackingTouch: &quot;)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

           
            override fun onProgressChanged(
                seekBar: SeekBar, progress: Int,
                fromUser: Boolean
            ) 
                  // here 

        })

huangapple
  • 本文由 发表于 2023年5月22日 12:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定