กำหนด step ให้เพิ่มขึ้นหรือลดลงทีละ 5
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?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=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/textViewProgress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" android:text="- -" android:textSize="24sp" /> <TextView android:id="@+id/seekBarStatus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="20dp" android:text="- -" android:textSize="24sp" /> <SeekBar android:id="@+id/seekBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp" /> </LinearLayout> </android.support.constraint.ConstraintLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.phaisarn.ktapplication import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.SeekBar import kotlinx.android.synthetic.main.activity_main.* // SeekBar Range const val MIN = 10 const val MAX = 100 const val STEP = 5 class MainActivity : AppCompatActivity(), SeekBar.OnSeekBarChangeListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // set max to seekbar seekBar!!.max = (MAX - MIN) / STEP seekBar!!.setOnSeekBarChangeListener(this) } override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) { val progress_custom = MIN + (progress * STEP) textViewProgress!!.text = progress_custom.toString() seekBarStatus!!.text = "Tracking Touch" } override fun onStartTrackingTouch(seekBar: SeekBar) { seekBarStatus!!.text = "Started Tracking Touch" } override fun onStopTrackingTouch(seekBar: SeekBar) { seekBarStatus!!.text = "Stopped Tracking Touch" } } |