【问题标题】:Change button text in new MaterialDatePicker更改新 MaterialDatePicker 中的按钮文本
【发布时间】:2020-04-04 20:38:48
【问题描述】:

我正在将日历组件迁移到新的材料库 MaterialDatePicker,但是我在做一些简单的事情时遇到了问题,比如更改确认按钮文本的名称

以前用这个就够了

datePickerDialog.setButton(DatePickerDialog.BUTTON_POSITIVE, getString(R.string.save), datePickerDialog)

但现在我找不到任何公共方法

val picker = Builder.datePicker()
            picker.addOnPositiveButtonClickListener { selection: Long ->
                saveListPosition()
                accountDetailsPresenter.editDate(selection, transaction, product, "")
            }
picker.show(childFragmentManager, picker.toString())

【问题讨论】:

    标签: android android-datepicker material-components material-components-android


    【解决方案1】:

    更新至1.4.0

    我们仍然没有公开的方法来为否定和肯定按钮提供自定义文本。出于某种原因,使用findViewById<Button>(com.google.android.material.R.id.confirm_button).text = "some text" 对我来说不起作用。相反,将以下行添加到您的字符串 xmls 以使用您的自定义文本。 (这将产生应用范围的影响!)这些和 @Gabriele Mariotti 提供的区别是更新的字符串资源 ID。

    <string description="Confirms the selection [CHAR_LIMIT=12]" name="mtrl_picker_save">Your positive button text</string>
    
    <string name="cancel">Your negative button text</string>
    

    【讨论】:

      【解决方案2】:

      有一种解决方法可以以编程方式设置按钮文本,最重要的是不是针对整个项目。

      1. 创建您的日期选择器

             val picker : MaterialDatePicker<*> = MaterialDatePicker.Builder.datePicker().build()
        
      2. 通过 id 查找正面或负面按钮。

             picker?.view?.findViewById<Button>(com.google.android.material.R.id.confirm_button)?.text =
                 "Your text here"
        
             picker?.view?.findViewById<Button>(com.google.android.material.R.id.cancel_button)?.text =
                 "Your text here"
        

      【讨论】:

      • 尝试检查 com.google.android.material.datepicker.MaterialDatePicker 的源代码,因为我的解决方案是在材料库 1.3.0 上完成的
      【解决方案3】:

      目前(1.1.0-beta021.2.0-alpha02)您无法以编程方式执行此操作。

      但是,您可以覆盖项目中的现有字符串,但此解决方法可能会在下一个版本中停止运行

      对于项目中的确认和取消按钮,请使用以下字符串:

        <string name="mtrl_picker_confirm" description="Button text to indicate that the widget will save the user's selection [CHAR_LIMIT=16]">.....</string>
        <string name="mtrl_picker_cancel" description="Button text to indicate that the widget will ignore the user's selection [CHAR_LIMIT=16]" translatable="false">...</string>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多