📅  最后修改于: 2021-01-05 08:29:05             🧑  作者: Mango
Android SeekBar是一种带有可拖动拇指的ProgressBar。最终用户可以左右拖动拇指来移动歌曲的进度,文件下载等。
SeekBar.OnSeekBarChangeListener接口提供了用于对搜索栏执行均匀处理的方法。
我们可以在Android中创建两种类型的SeekBar
使用属性style =“ @ style / Widget.AppCompat.SeekBar.Discrete”创建SeekBar的离散进度点,并使用属性max指定最大值。
在此示例中,我们将创建具有平滑进度的SeekBar和具有离散进度点的SeekBar。
在activity_main.xml布局中添加SeekBar。
在此类中,我们调用SeekBar.OnSeekBarChangeListener接口并覆盖其方法。
package example.javatpoint.com.kotlinseekbar
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.SeekBar
import android.widget.Toast
class MainActivity : AppCompatActivity() {
var seekBarNormal: SeekBar? = null
var seekBarDiscrete: SeekBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
seekBarNormal= findViewById(R.id.seekbar_Default)
seekBarDiscrete = findViewById(R.id.seekbar_Discrete)
seekBarNormal?.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int,
fromUser: Boolean) {
Toast.makeText(applicationContext, "seekbar progress: $progress", Toast.LENGTH_SHORT).show()
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
Toast.makeText(applicationContext, "seekbar touch started!", Toast.LENGTH_SHORT).show()
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
Toast.makeText(applicationContext, "seekbar touch stopped!", Toast.LENGTH_SHORT).show()
}
})
seekBarDiscrete?.setOnSeekBarChangeListener(object :SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(applicationContext, "discrete seekbar progress: $progress", Toast.LENGTH_SHORT).show()
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(applicationContext, "discrete seekbar touch started!", Toast.LENGTH_SHORT).show()
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(applicationContext, "discrete seekbar touch stopped", Toast.LENGTH_SHORT).show()
}
})
}
}
输出: