首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Kotlin片段中实现setOnSeekBarChangeListener

如何在Kotlin片段中实现setOnSeekBarChangeListener
EN

Stack Overflow用户
提问于 2022-03-29 14:37:26
回答 1查看 1.3K关注 0票数 1

我目前正在我的uni类中开发一个移动应用程序,该应用程序利用seekBar让用户决定使用计时器进行测试。此应用程序使用main托管所有片段。目前,我只想在文本框中显示用户滚动搜索栏的位置,但是很难找到解决方案。如有任何建议,将不胜感激。这是我在TitleFragment.kt中的代码

代码语言:javascript
运行
复制
package com.example.android.guesstheword.screens.title

import android.os.Bundle
import android.view.DragEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.databinding.adapters.SeekBarBindingAdapter
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.TitleFragmentBinding
import kotlinx.android.synthetic.main.title_fragment.*

/**
 * Fragment for the starting or title screen of the app
 */
class TitleFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View {

        // Inflate the layout for this fragment
        val binding: TitleFragmentBinding = DataBindingUtil.inflate(
                inflater, R.layout.title_fragment, container, false)

        binding.timerSwitch.setOnClickListener {

            if(binding.timerSwitch.isChecked){
                binding.timerBar.visibility = View.VISIBLE
            }
            else{
                binding.timerBar.visibility = View.INVISIBLE
            }
        }

        val seekBar = binding.timerBar
        seekBar.setOnSeekBarChangeListener(binding)
        binding.playGameButton.setOnClickListener {

            findNavController().navigate(TitleFragmentDirections.actionTitleToGame())
        }
        return binding.root
    }
}

private fun SeekBar.setOnSeekBarChangeListener(binding: TitleFragmentBinding) {
    val seconds = binding.timerSeconds
    seconds.visibility = View.VISIBLE
    seconds.text = binding.timerBar.progress.toString()
}

title_fragment.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?><!--
<layout 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/title_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".screens.title.TitleFragment">

        <TextView
            android:id="@+id/get_ready_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:fontFamily="sans-serif"
            android:text="@string/get_ready"
            android:textColor="@color/black_text_color"
            android:textSize="14sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@+id/title_text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:fontFamily="sans-serif"
            android:text="@string/title_text"
            android:textColor="@color/black_text_color"
            android:textSize="34sp"
            android:textStyle="normal"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/get_ready_text"
            app:layout_constraintVertical_chainStyle="packed" />

        <Button
            android:id="@+id/play_game_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="32dp"
            android:text="@string/play_button"
            android:theme="@style/GoButton"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <Switch
            android:id="@+id/timer_switch"
            android:layout_width="95dp"
            android:layout_height="52dp"
            android:text="Timer"
            android:min="5"
            android:max="360"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title_text"
            app:layout_constraintVertical_bias="0.064"/>

        <SeekBar
            android:id="@+id/timerBar"
            android:layout_width="132dp"
            android:layout_height="85dp"
            android:clickable="false"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/timer_switch"
            app:layout_constraintVertical_bias="0.144" />

        <TextView
            android:id="@+id/timer_seconds"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone"
            app:layout_constraintBottom_toTopOf="@+id/play_game_button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/timerBar"
            app:layout_constraintVertical_bias="0.17" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-29 14:48:35

您创建了一个名为setOnSeekBarChangeListener的扩展函数,它实际上并没有设置它。

为了设置侦听器,您需要执行如下操作:

代码语言:javascript
运行
复制
binding.timerBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        // here, you react to the value being set in seekBar
    }

    override fun onStartTrackingTouch(seekBar: SeekBar) {
        // you can probably leave this empty
    }

    override fun onStopTrackingTouch(seekBar: SeekBar) {
        // you can probably leave this empty
    }
})

但是请记住,这不是您创建的扩展函数。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71664074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档