【发布时间】: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