如何在kotlin片段中setonsonsondonsondonsondensekbarchangelistener

发布于 2025-01-17 13:08:45 字数 6547 浏览 1 评论 0原文

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

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

<?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>

I am currently working on a mobile application in my uni classes that utilizes a seekBar to let the user decide to use a timer for a quiz. This application uses main to host all of the fragments. currently I just want the textbox to display where the user scrolled the seek bar to but am struggling to find a solution. any advice would be greatly appreciated. this is the code I have within the
TitleFragment.kt:

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:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花海 2025-01-24 13:08:45

您创建了一个称为setondensekbarchangelistener的扩展功能,该功能实际上并未设置它。

为了设置侦听器,您需要做类似的事情:

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
    }
})

但请记住,这不是您创建的扩展功能。

You created an extension function called setOnSeekBarChangeListener which does not actually set it.

In order to set the listener, you need to do something like this:

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
    }
})

But keep in mind that this is NOT the extension function you created.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文