【问题标题】:how to save the value of a SeekBar and pass it to other activities?如何保存 SeekBar 的值并将其传递给其他活动?
【发布时间】:2019-02-21 21:44:18
【问题描述】:

我是 kotlin 语言的新手,我正在尝试在我的设置活动中实现 SeekBar 以调整 TextViews 的大小,并通过共享首选项保存其值,然后将 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
    }
}

【问题讨论】:

    标签: android kotlin sharedpreferences


    【解决方案1】:

    您需要确保在移动 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。

    【讨论】:

    • 这取决于你如何使用它。您可以使用 0 到 100,这样可以轻松转换为百分比。
    • 它不起作用怎么办?它会保存该值,您是否正确使用了该值?
    • 我复制了您的代码,但它不起作用我的朋友,当我将 SeekBar 设置为 50 或 30..etc 时,当我进入主要活动时,共享首选项不会保存 SeekBar 值,并且返回设置活动,它仍然是相同的值,即 20>
    • @Abood 您应该调试它,确保在移动时将正确的值写入您的 SeekBar。如果文本正在更新,那么它正在保存。你有可以重置值的代码,我在我的例子中删除了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多