Android: colorPicker setButtonOkText 无效

Android: colorPicker setButtonOkText not worked

我有颜色选择器,但我需要更改文本 setButtonOkText(R.string.accept)setButtonCancelText(R.string.cancel) 但不工作只有颜色选择器使用默认确定并取消 可以改变背景托盘颜色吗?或为 ok 取消文本设置字体?

  <resources>
    <string name="cancel">Close</string>
    <string name="accept">Accept</string>
    </resources>

        val colorPicker=ColorPickerDialog.Builder()
            .setInitialColor(Color.BLACK)
            .setColorModel(ColorModel.HSV)
            .setColorModelSwitchEnabled(true)
            .setButtonOkText(R.string.accept)
            .setButtonCancelText(R.string.cansel)
            .onColorSelected { color: Int ->

            }
            .create()
        colorPicker.show(supportFragmentManager, "color_picker")

不幸的是,这个库看起来有一个错误。

正面和负面按钮的文本永远不会设置为传入的 ID。

https://github.com/vadiole/colorpicker/blob/master/colorpicker/src/main/java/vadiole/colorpicker/ColorPickerView.kt

查看第 201 行和第 202 行,这是唯一一次拉动这 2 个按钮并且它们都没有调用 setText。 xml 表明它们被硬编码为默认字符串。

幸运的是这里可​​能有解决方法,

https://github.com/vadiole/colorpicker/blob/master/colorpicker/src/main/java/vadiole/colorpicker/ColorPickerDialog.kt

您需要覆盖两个 类 ColorPickerViewColorPickerDialog

ColorPickerView 将只需要您提供修改后的构造函数。称之为 FixedColorPickerView

constructor(
    context: Context,
    actionOkRes: Int,
    actionCancelRes: Int,
    @ColorInt initialColor: Int = Color.DKGRAY,
    colorModel: ColorModel,
    colorModelSwitchEnabled: Boolean,
    onSwitchColorModelListener: OnSwitchColorModelListener? = null,
) : super(context,
          actionOkRes,
          actionCancelRes,
          initialColor,
          colorModel,
          colorModelSwitchEnabled,
          onSwitchColorModelListener) {
            //Your constructor will simply grab the buttons and set their texts 
            //after the real constructor has inited view.
            val positiveButton = findViewById<Button>(R.id.positive_button)
            val negativeButton = findViewById<Button>(R.id.negative_button)
            positiveButton.setText(actionOkRes)
            negativeButton.setText(actionCancelRes)
}

ColorPickerDialog 你需要覆盖 2 个方法

onCreateDialog 替换ColorPickerView 其创建。

onSaveInstanceState 以避免 NPE 由于引用旧的 ColorPickerView

class FixedColorPickerDialog : ColorPickerDialog {
    var fixPickerView: ColorPickerView by Delegates.notNull()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val bundle = savedInstanceState ?: arguments!!
        val actionOk = bundle.getInt(ACTION_OK_KEY)
        val actionCancel = bundle.getInt(ACTION_CANCEL_KEY)

        fixPickerView =
            FixedColorPickerView(
                requireContext(),
                actionOk,
                actionCancel,
                bundle.getInt(INITIAL_COLOR_KEY),
                ColorModel.fromName(bundle.getString(COLOR_MODEL_NAME_KEY)),
                bundle.getBoolean(COLOR_MODEL_SWITCH_KEY),
                onSwitchColorModelListener
            )

        fixPickerView.enableButtonBar(
            object : ColorPickerView.ButtonBarListener {
                override fun onNegativeButtonClick() = dismiss()
                override fun onPositiveButtonClick(color: Int) {
                    onSelectColorListener?.onColorSelected(color)
                    dismiss()
                }
            }
        )

        return AlertDialog.Builder(requireContext()).setView(fixPickerView).create()
    }
    override fun onSaveInstanceState(outState: Bundle) {
        val bundle =
            makeArgs(
                fixPickerView.actionOkRes,
                fixPickerView.actionCancelRes,
                fixPickerView.currentColor,
                fixPickerView.colorModel,
                fixPickerView.colorModelSwitchEnabled
            )
        outState.putAll(bundle)
        super.onSaveInstanceState(outState)
    }
}