簡體   English   中英

你如何找到沒有點擊的單選按鈕?

[英]How do you find the radio buttons not clicked?

我有一個無線電組用於設置數據類的屬性,特別是三明治。 到目前為止,我的方法是這樣的:如果選中了三明治,我手動設置三明治可見,如果沒有選擇三明治,則手動設置三明治不可見。 我想通過使用一個函數來簡化它,但我不確定如何找到未選擇的單選按鈕 ID。 我是 Kotlin 和 Android 的新手,所以任何幫助將不勝感激。

fun onSandwichRadioButtonClicked(view: View) {
        if (view is RadioButton) {
            // Is the button now checked?
            val checked = view.isChecked

            // Check which radio button was clicked
            when (view.getId()) {
                R.id.panini_button ->
                    if (checked) {
                        sandwich.sandwichCost = 7.0f
                        sandwich.name = "Panini"
                        binding.panini.visibility = View.VISIBLE
                        binding.hoagie.visibility = View.GONE
                        binding.sandwich.visibility = View.GONE
                    }
                R.id.hoagie_button ->
                    if (checked) {
                        sandwich.sandwichCost = 10.0f
                        sandwich.name = "Hoagie"
                        binding.panini.visibility = View.GONE
                        binding.hoagie.visibility = View.VISIBLE
                        binding.sandwich.visibility = View.GONE
                    }
                R.id.sandwich_button ->
                    if (checked) {
                        sandwich.sandwichCost = 5.0f
                        sandwich.name = "Sandwich"
                        binding.panini.visibility = View.GONE
                        binding.hoagie.visibility = View.GONE
                        binding.sandwich.visibility = View.VISIBLE
                    }
            }
            sandwich.totalCost = (sandwich.extraCost + sandwich.sandwichCost).toString()
            binding.invalidateAll()
            Toast.makeText(
                activity, "Total Cost :" +
                        " ${sandwich.totalCost}",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    fun setSandwich(sandwichName: String, sandwichCost: Float, sandwichImage: ImageView){
        sandwich.sandwichCost = sandwichCost
        sandwich.name = sandwichName
        sandwichImage.visibility = View.VISIBLE
        //set the remaining sandwiches to invisible. how do I find these sandwiches?

相關的xml代碼:

 <ImageView
            android:id="@+id/panini"
            android:layout_width="192dp"
            android:layout_height="79dp"
            android:contentDescription="TODO"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.411"
            app:srcCompat="@drawable/ic_food_obvious_panini"
            tools:visibility="visible" />

        <ImageView
            android:id="@+id/hoagie"
            android:layout_width="149dp"
            android:layout_height="153dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.503"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.394"
            app:srcCompat="@drawable/hoagie"
            tools:visibility="gone" />

        <ImageView
            android:id="@+id/sandwich"
            android:layout_width="140dp"
            android:layout_height="311dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.345"
            app:srcCompat="@drawable/ic_sandwhichanddrink"
            tools:visibility="visible" />


        <RadioGroup
            android:id="@+id/sandwichRadioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintBottom_toTopOf="@+id/tomatoes_check"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.88">

            <RadioButton
                android:id="@+id/sandwich_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onSandwichRadioButtonClicked"
                android:text="@string/sandwich"
                android:textSize="24sp" />

            <RadioButton
                android:id="@+id/hoagie_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onSandwichRadioButtonClicked"
                android:text="@string/hoagie"
                android:textSize="24sp" />

            <RadioButton
                android:id="@+id/panini_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="onSandwichRadioButtonClicked"
                android:text="@string/panini"
                android:textSize="24sp" />
        </RadioGroup>

更新:我已經簡化了一點,但我覺得它仍然可以更好。 圖像視圖的 ArrayList 是我當前的解決方法:

fun onSandwichRadioButtonClicked(view: View) {

        var notChosenSandwiches: ArrayList<ImageView> = ArrayList()
        if (view is RadioButton) {
            // Is the button now checked?
            val checked = view.isChecked

            // Check which radio button was clicked
            when (view.getId()) {
                R.id.panini_button ->
                    if (checked) {
                        notChosenSandwiches.clear()
                        notChosenSandwiches.add(binding.hoagie)
                        notChosenSandwiches.add(binding.sandwich)
                        setSandwich("Panini", 7.0f, binding.panini, notChosenSandwiches)
                    }
                R.id.hoagie_button ->
                    if (checked) {
                        notChosenSandwiches.clear()
                        notChosenSandwiches.add(binding.panini)
                        notChosenSandwiches.add(binding.sandwich)
                        setSandwich("Hoagie", 10.0f, binding.hoagie, notChosenSandwiches)
                    }
                R.id.sandwich_button ->
                    if (checked) {
                        notChosenSandwiches.clear()
                        notChosenSandwiches.add(binding.hoagie)
                        notChosenSandwiches.add(binding.panini)
                        setSandwich("Melt", 5.0f, binding.sandwich, notChosenSandwiches)
                    }
            }
            sandwich.totalCost = (sandwich.extraCost + sandwich.sandwichCost).toString()
            binding.invalidateAll()
            Toast.makeText(
                activity, "Total Cost :" +
                        " ${sandwich.totalCost}",
                Toast.LENGTH_SHORT
            ).show()
        }
    }

    /**
     * update the sandwich and the views
     *
     * @param sandwichName the new name of the sandwich
     * @param sandwichCost the new cost of the sandwich
     * @param shownImage the image of the sandwich
     * @param hideThese the not chosen sandwiches
*/
    fun setSandwich(sandwichName: String, sandwichCost: Float, shownImage: ImageView, hideThese: ArrayList<ImageView>){
        sandwich.sandwichCost = sandwichCost
        sandwich.name = sandwichName
        shownImage.visibility = View.VISIBLE
        for (image in hideThese)
            image.visibility = View.GONE
    }

您的問題是您知道所選按鈕的 ID,並且想要列出所有其他(未選擇的)ID,以便您可以對它們做一些有用的事情嗎?

看起來RadioGroup不會為您提供其按鈕列表(只是當前選擇了哪個),因此您可能必須自己生成。 它是LinearLayout的一個子類,所以它沒有做任何復雜的事情,它只是將所有RadioButton作為直接子級,所以你可以這樣做:

sandwichButtons = sandwichRadioGroup.children
    .filterIsInstance(RadioButton::class.java).toList()

這給你一個List<RadioButton> 或者,如果您只想要他們的 ID:

    sandwichButtonIds = sandwichRadioGroup.children
    .filterIsInstance(RadioButton::class.java)
    .map { it.id }
    .toList()

這是一個List<Int>及其所有 ID( R.id.hoagie值)。 如果您將其設置為lateinit var字段,並在onCreateonViewCreated或其他期間使用查找進行分配,您將擁有一個包含組中所有單選按鈕的不錯列表。 然后你可以做這樣的事情:

val unselected = sandwichButtonIds.minus(selectedId)

可能有很多事情可以幫助您完成工作,但我現在只能猜測,所以希望這是一個開始! “數據類,特別是三明治”也得到了我的支持

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM