将图标切换为文本到ToggleGroup?

huangapple go评论67阅读模式
英文:

How to switch icon to text into ToggleGroup?

问题

以下是翻译好的部分:

有一个包含4个MaterialButtonMaterialButtonToggleGroup,且水平排列。某些设备的屏幕可能无法显示所有按钮在同一行。因此,我想只显示图标,但对于选定的按钮,添加文本。是否可以通过样式实现?最好带有动画效果。

现在:

将图标切换为文本到ToggleGroup?

目标:

将图标切换为文本到ToggleGroup?

将图标切换为文本到ToggleGroup?

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/rename_toggle_group"
    style="@style/CustomButtonToggleGroup"
    android:orientation="horizontal"
    android:layout_marginTop="@dimen/view_indent_vertical_min"
    android:gravity="center"
    app:checkedButton="@id/rename_current"
    app:selectionRequired="true"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_current"
        android:minWidth="0dp"
        style="@style/MaterialButtonSecondaryToggle"
        app:icon="@drawable/ic_general"
        android:text="当前" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_top"
        android:minWidth="0dp"
        app:icon="@drawable/ic_general"
        style="@style/MaterialButtonSecondaryToggle" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_bottom"
        android:minWidth="0dp"
        style="@style/MaterialButtonSecondaryToggle"
        app:icon="@drawable/ic_general"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_all"
        android:minWidth="0dp"
        style="@style/MaterialButtonSecondaryToggle"
        app:icon="@drawable/ic_general" />

</com.google.android.material.button.MaterialButtonToggleGroup>
英文:

There is MaterialButtonToggleGroup with 4 MaterialButton and horizontal orientation. Some device's screens can't show all buttons in row. Thus I want to show only icons, but for selected button add text. Is it possible with style? Preferably with animation.

Now:

将图标切换为文本到ToggleGroup?

Target:

将图标切换为文本到ToggleGroup?

将图标切换为文本到ToggleGroup?

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/rename_toggle_group"
    style="@style/CustomButtonToggleGroup"
    android:orientation="horizontal"
    android:layout_marginTop="@dimen/view_indent_vertical_min"
    android:gravity="center"
    app:checkedButton="@id/rename_current"
    app:selectionRequired="true"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_current"
        android:minWidth="0dp"
        style="@style/MaterialButtonSecondaryToggle"
        app:icon="@drawable/ic_general"
        android:text="Current" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_top"
        android:minWidth="0dp"
        app:icon="@drawable/ic_general"
        style="@style/MaterialButtonSecondaryToggle" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_bottom"
        android:minWidth="0dp"
        style="@style/MaterialButtonSecondaryToggle"
        app:icon="@drawable/ic_general"/>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/rename_all"
        android:minWidth="0dp"
        style="@style/MaterialButtonSecondaryToggle"
        app:icon="@drawable/ic_general" />

</com.google.android.material.button.MaterialButtonToggleGroup>

答案1

得分: 1

我尝试过这个,对我起了作用。

    private fun setupToggleGroup() {
        val (checkedButton, text) = getTogglePair(binding.renameToggleGroup.checkedButtonId)
        checkedButton?.text = text.toString()

        binding.renameToggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked ->
            val (button, text) = getTogglePair(checkedId)
            button?.text = if (isChecked) text.toString() else ""
        }
    }

    private fun getTogglePair(checkedButtonId: Int): Pair<MaterialButton?, String?> {
        return when (checkedButtonId) {
            binding.renameCurrent.id -> Pair(binding.renameCurrent , getString(R.string.rename_current))
            binding.renameTop.id -> Pair(binding.renameTop , getString(R.string.rename_top))
            binding.renameBottom.id -> Pair(binding.renameBottom , getString(R.string.rename_bottom))
            binding.renameAll.id -> Pair(binding.renameAll , getString(R.string.rename_all))
            else -> Pair(null, null)
        }
    }
调用 setupToggleGroup() 在 Activity 的 onCreate 中
英文:

I tried this and worked for me

private fun setupToggleGroup() {
    val (checkedButton, text) = getTogglePair(binding.renameToggleGroup.checkedButtonId)
    checkedButton?.text = text.toString()

    binding.renameToggleGroup.addOnButtonCheckedListener { group, checkedId, isChecked -&gt;
        val (button, text) = getTogglePair(checkedId)
        button?.text = if (isChecked) text.toString() else &quot;&quot;
    }
}

private fun getTogglePair(checkedButtonId: Int): Pair&lt;MaterialButton?, String?&gt; {
    return when (checkedButtonId) {
        binding.renameCurrent.id -&gt; Pair(binding.renameCurrent , getString(R.string.rename_current))
        binding.renameTop.id -&gt; Pair(binding.renameTop , getString(R.string.rename_top))
        binding.renameBottom.id -&gt; Pair(binding.renameBottom , getString(R.string.rename_bottom))
        binding.renameAll.id -&gt; Pair(binding.renameAll , getString(R.string.rename_all))
        else -&gt; Pair(null, null)
    }
}

call setupToggleGroup() in onCreate of the Activity

huangapple
  • 本文由 发表于 2023年4月11日 14:23:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定