شرح التعامل مع SeekBar لاستخدامها داخل التطبيق في لغة kotlin
اولاً نقوم باضافة العنصر الى layout الخاص في الواجهة
1 2 3 4 5 |
<SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"/> |
تلقائياً يكون من 0 الى 100 يمكن تغير القيمة عن طريق max value
1 |
android:max |
مثلاً
1 |
android:max="10" |
سوف يصبح من 0 الى 10
ال event الخاص به لاخذ القيمة النهائية او وهوا المستخدم يختار
1 2 3 4 5 6 7 8 9 10 11 |
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { // called when progress is changed } override fun onStartTrackingTouch(seekBar: SeekBar?) { // called when tracking the seekBar is started } override fun onStopTrackingTouch(seekBar: SeekBar?) { // called when tracking the seekBar is stopped } |
سوف نقوم بتغير قيمة textview داخل دالة onProgressChanged لاظهار القيمة والمستخدم يحرك لاختيار القيمة
واخيراً يتم جلب القيمة النهائية عند ضغط المستخدم على button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{ override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { // called when progress is changed text_view.text = progress.toString() } override fun onStartTrackingTouch(seekBar: SeekBar?) { // called when tracking the seekBar is started } override fun onStopTrackingTouch(seekBar: SeekBar?) { // called when tracking the seekBar is stopped } }) btn.setOnClickListener({ showToast(seek_bar.progress.toString()) }) |
كود 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 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.kotdroid.testkotlin.MainActivity"> <SeekBar android:id="@+id/seek_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="10" android:layout_margin="10dp"/> <TextView android:id="@+id/text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Click" /> </LinearLayout> |
كود kotlin كاملاً
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 |
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) seek_bar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{ override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { // called when progress is changed text_view.text = progress.toString() } override fun onStartTrackingTouch(seekBar: SeekBar?) { // called when tracking the seekBar is started } override fun onStopTrackingTouch(seekBar: SeekBar?) { // called when tracking the seekBar is stopped } }) btn.setOnClickListener({ showToast(seek_bar.progress.toString()) }) } fun showToast(message: String) { Toast.makeText(applicationContext, message, Toast.LENGTH_LONG).show() } } |