我在 Kotlin 中使用带有搜索栏的 alertDialog 框。搜索栏有效,但我无法更新 textView
I am using an alertDialog box with a seekbar in Kotlin. The seekbar works but I can't update the textView
**我正在尝试使用搜索栏来使用警报对话框。搜索栏有效,但我无法更新 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()
}
}
当 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()
}
**我正在尝试使用搜索栏来使用警报对话框。搜索栏有效,但我无法更新 textView - (tvPlayers)。最初我只使用了一个 editText 框并且它起作用了,但如果我可以使用一个搜索栏就更好了。
选择肯定按钮后,变量 numberOfPlayers 会存储搜索栏进度,但我无法在移动搜索栏拇指时跟踪位置。
我在 Android Studio 3.1
中使用 Kotlin 和 viewBindingedit_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()
}