我正在使用带有 Kotlin 搜索栏的警报对话框。搜索栏有效,但我无法更新文本视图



**我正试图使用seekbar使用alertDialog框。搜索栏有效,但我无法更新文本查看-(tvPlayers(。最初我只是用了一个editText框,这很有效,但如果我能用一个搜索栏就好了。

变量numberOfPlayers在选择正按钮后存储搜索杆进度,但当搜索杆拇指移动时,我无法跟踪位置。

我正在使用Kotlin和viewBinding与Android Studio 3.1

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

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

最新更新