英文:
How to switch icon to text into ToggleGroup?
问题
以下是翻译好的部分:
有一个包含4个MaterialButton的MaterialButtonToggleGroup,且水平排列。某些设备的屏幕可能无法显示所有按钮在同一行。因此,我想只显示图标,但对于选定的按钮,添加文本。是否可以通过样式实现?最好带有动画效果。
现在:
目标:
<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:
Target:
<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 ->
        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)
    }
}
call setupToggleGroup() in onCreate of the Activity
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。





评论