【问题标题】:I am using an alertDialog box with a seekbar in Kotlin. The seekbar works but I can't update the textView我在 Kotlin 中使用带有搜索栏的 alertDialog 框。搜索栏有效,但我无法更新 textView
【发布时间】:2022-01-10 11:45:31
【问题描述】:

**我正在尝试通过搜索栏使用警报对话框。搜索栏有效,但我无法更新 textView - (tvPlayers)。最初我只使用了一个 editText 框,它可以工作,但如果我可以使用搜索栏就更好了。

一旦选择了正按钮,变量 numberOfPlayers 就会存储搜索栏进度,但是当搜索栏拇指移动时,我无法跟踪位置。

我在 Android Studio 3.1 中使用 Kotlin 和 viewBinding

edit_textnumberplayers

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tvPlayers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:gravity="center"
        android:text="0"
        android:textSize="16sp" />

    <SeekBar
        android:id="@+id/etNumberOfPlayers"
        style="@android:style/Widget.DeviceDefault.SeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:max="10"
        android:min="1"
        android:progress="1"
        android:progressTint="#0710B8"
        android:thumb="@drawable/ic_baseline_person_add_alt_1_24" />


    <!--<EditText
        android:id="@+id/etNumberOfPlayers"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:hint="Select number of players"
        android:inputType="number"
        android:textSize="16sp" />-->


</LinearLayout>

initialSetupScreen.kt

    private fun chooseNumberOfPlayers() {
        val builder = AlertDialog.Builder(this)
        val inflater = layoutInflater
        val dialogLayout = inflater.inflate(R.layout.edit_text_number_players, null)
        val seekBar = dialogLayout.findViewById<SeekBar>(R.id.etNumberOfPlayers)

        with(builder) {
            setTitle("How many players do you need")

            binding2.tvPlayers.text = seekBar.progress.toString()

            setPositiveButton("OK") { dialog, which ->
                numberOfPlayers = seekBar.progress
                // Show the correct number of enter name boxes.
                showPlayerNameTextBoxes()
            }

            setNegativeButton("Cancel") { dialog, which ->
                Log.d("Main", "Negative button clicked")
                hidePlayerNameTextBoxes()
                finish() // Return to MainMenu screen.
            }
            setView(dialogLayout)
            show()
        }
    }

【问题讨论】:

    标签: android android-studio kotlin android-alertdialog seekbar


    【解决方案1】:

    当 Seekbar 的值发生变化时,您应该更新 textView。
    例如:

    with(builder) {
        setTitle("How many players do you need")
    
        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seek, progress, fromUser) {
              binding2.tvPlayers.text = seekBar.progress.toString()
            }
      
            override fun onStartTrackingTouch(seek) {}
            override fun onStopTrackingTouch(seek) {}
        })
    
        setPositiveButton("OK") { dialog, which ->
            numberOfPlayers = seekBar.progress
            // Show the correct number of enter name boxes.
            showPlayerNameTextBoxes()
        }
        setNegativeButton("Cancel") { dialog, which ->
            Log.d("Main", "Negative button clicked")
            hidePlayerNameTextBoxes()
            finish() // Return to MainMenu screen.
        }
        setView(dialogLayout)
        show()
    }
    

    【讨论】:

    • 感谢您的帮助@DarShan。 tvPlayers.text 仍未更新。可能与 binding2 设置有关。
    • private lateinit var binding: ActivityInitialSetupScreenBinding private lateinit var binding2: EditTextNumberPlayersBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityInitialSetupScreenBinding.inflate(layoutInflater) setContentView(binding.root) binding2 = EditTextNumberPlayersBinding.inflate(layoutInflater) setContentView(binding.root)
    • 解决了 - @DarShan 我只需要在函数中声明 'val player = dialogLayout.findViewById(R.id.tvPlayers)' 然后将 ib onProgressChanged 行更改为 'player. text = seek.progress.toString()' 再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-22
    相关资源
    最近更新 更多