如何保存SeekBar的值并将其传递给其他活动?

时间:2019-02-21 21:44:18

标签: android kotlin sharedpreferences

我是Kotlin语言的新手,正在尝试实现SeekBar来调整我的设置活动中TextView的大小,并通过共享首选项保存其值,然后将SeekBar的值传递给主要活动!

我尝试了几个答案,但似乎没有一个对我有用!

设置活动

 @RequiresApi(Build.VERSION_CODES.O)
fun seekbarFontSize(){

    var savedProgress1:Int = 0
    val pref = PreferenceManager.getDefaultSharedPreferences(this)
    val editor = pref.edit()
    savedProgress1 = seekFontSize.progress
    editor.putInt("seekFonts", savedProgress1)
    editor.apply()
    seekFontSize.min = 20
    seekFontSize.max = 80
    seekbarCounter.text = "20"



        seekFontSize.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
       /**
        * Notification that the progress level has changed. Clients can use the fromUser parameter
        * to distinguish user-initiated changes from those that occurred programmatically.
        *
        * @param seekBar The SeekBar whose progress has changed
        * @param progress The current progress level. This will be in the range min..max where min
        * and max were set by [ProgressBar.setMin] and
        * [ProgressBar.setMax], respectively. (The default values for
        * min is 0 and max is 100.)
        * @param fromUser True if the progress change was initiated by the user.
        */
       override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
           textFont.textSize = progress.toFloat()
           textFont1.textSize = progress.toFloat()
           seekbarCounter.text = "$progress"

       }


       /**
        * Notification that the user has started a touch gesture. Clients may want to use this
        * to disable advancing the seekbar.
        * @param seekBar The SeekBar in which the touch gesture began
        */
       override fun onStartTrackingTouch(seekBar: SeekBar?) {
           Toast.makeText(this@SettingActivity, "start tracking",Toast.LENGTH_LONG).show()

       }

       /**
        * Notification that the user has finished a touch gesture. Clients may want to use this
        * to re-enable advancing the seekbar.
        * @param seekBar The SeekBar in which the touch gesture began
        */
       override fun onStopTrackingTouch(seekBar: SeekBar?) {
           Toast.makeText(this@SettingActivity, "stop tracking",Toast.LENGTH_LONG).show()
       }

   })

MainAcitvity

fun seekbarSizing(){
    val pref = PreferenceManager.getDefaultSharedPreferences(this).getInt("seek", 0)
    if (pref == 0){
        TitleEt.textSize = 20f
    }
    if (pref == 1){
        TitleEt.textSize = 50f
    }
}

1 个答案:

答案 0 :(得分:0)

您需要确保在移动SeekBar时更新值。将您的存储逻辑移至onProgressChanged方法中即可。

@RequiresApi(Build.VERSION_CODES.O)
fun seekbarFontSize() {

  seekFontSize.min = 20
  seekFontSize.max = 80
  seekbarCounter.text = "20"

  seekFontSize.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
    override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
      textFont.textSize = progress.toFloat()
      textFont1.textSize = progress.toFloat()
      seekbarCounter.text = "$progress"

      // You need to save the current progress when the SeekBar is changed.
      val pref = PreferenceManager.getDefaultSharedPreferences(this)
      editor.putInt("seekFonts", savedProgress1)
      editor.apply()
    }

    // ... other methods
  }

})

请注意,您要保存的数值介于20到80之间,而检查仅适用于0到1。您可以在SeekBar上使用不同的最小/最大值,以便更轻松地计算尺寸,除非您只是想将实际值用作字体大小,例如80。