繁体   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