个性化阅读
专注于IT技术分析

Kotlin Android SeekBar

Android SeekBar是一种带有可拖动拇指的ProgressBar。最终用户可以左右拖动拇指来移动歌曲的进度, 文件下载等。

SeekBar.OnSeekBarChangeListener接口提供了用于对搜索栏进行均匀处理的方法。

我们可以在Android中创建两种类型的SeekBar

  • SeekBar进展顺利。
  • SeekBar具有离散的进度点。

使用属性style =“ @ style / Widget.AppCompat.SeekBar.Discrete”创建SeekBar的离散进度点, 并使用属性max指定最大值。

Kotlin Android SeekBar示例

在此示例中, 我们将创建具有平滑进度的SeekBar和具有离散进度点的SeekBar。

activity_main.xml

在activity_main.xml布局中添加SeekBar。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="example.srcmini02.com.kotlinseekbar.MainActivity">


    <SeekBar
        android:id="@+id/seekbar_Default"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.174" />

    <SeekBar
        android:id="@+id/seekbar_Discrete"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:max="10"
        android:progress="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.333" />
</android.support.constraint.ConstraintLayout>

MainActivity.kt

在此类中, 我们调用SeekBar.OnSeekBarChangeListener接口并覆盖其方法。

package example.srcmini02.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()
            }
        })
    }
}

输出:

Kotlin Android SeekBar
Kotlin Android SeekBar
Kotlin Android SeekBar
Kotlin Android SeekBar
Kotlin Android SeekBar
Kotlin Android SeekBar
赞(0)
未经允许不得转载:srcmini » Kotlin Android SeekBar

评论 抢沙发

评论前必须登录!