kotlin 如何修复时间选择器对话框显示在西班牙语

k0pti3hp  于 2023-02-24  发布在  Kotlin
关注(0)|答案(3)|浏览(166)
    • bounty将在4天后过期**。回答此问题可获得+50的声誉奖励。MARSH正在寻找来自声誉良好来源的答案:请帮助我,这是发生,因为本地化,但我有调试代码,我看到它在英语语言,但它显示西班牙语

这是时间选择器的弹出窗口。

当我点击键盘图标时,它显示如下屏幕。

我在我们的应用程序和下面的代码中使用本地化,我已经编写了打开时间选择器弹出窗口

private fun openTimePickerDialog(datetime: String) {
          val mHour: Int = datetime.substring(8, 10).toInt()
          val mMinute: Int = datetime.substring(10, 12).toInt()
          setDefaultLanguage()
          Log.i("TAG", "openTimePickerDialog: $mHour $mMinute")
          val timePickerDialog = TimePickerDialog(
              activity,
              { _, hourOfDay, minute ->
                  var selectedMinute = minute.toString()
                  var selectedHour = hourOfDay.toString()
                  if (minute < 10) {
                      selectedMinute = "0$selectedMinute"
                  }
                  if (hourOfDay < 10) {
                      selectedHour = "0$selectedHour"
                  }
                  isClickedDate = ""
                  val date: String = datetime.substring(0, 8)
                  val time = selectedHour + selectedMinute + "00"
                  Log.i("TAG", "onTimeSet: $date$time")
                  roveR3SettingFragmentViewModel.setDateTime(date + time)
                  // txtTime.setText(hourOfDay + ":" + minute);
              }, mHour, mMinute, false
          )
          timePickerDialog.setButton(
              DatePickerDialog.BUTTON_POSITIVE,
              getString(R.string.ok),
              timePickerDialog
          )
          timePickerDialog.setButton(
              DatePickerDialog.BUTTON_NEGATIVE,
              getString(R.string.txt_cancel),
              timePickerDialog
          )
          timePickerDialog.show()
      }
   

  private fun setDefaultLanguage() {
          if (appPreference.appLanguage == LANGAUGE_ES) {
              val locale1 = Locale("en")
              Locale.setDefault(locale1)
              val config = requireContext().resources.configuration
              config.setLocale(locale1)
              requireContext().createConfigurationContext(config)
          } else if (appPreference.appLanguage == LANGAUGE_ES) {
              val locale1 = Locale("ja")
              Locale.setDefault(locale1)
              val config = requireContext().resources.configuration
              config.setLocale(locale1)
              requireContext().createConfigurationContext(config)
          }
      }

任何人可以请帮助我如何解决这是未来的语言问题,当我点击时间选择器对话框的键盘图标。我想在英语语言,我已经选择了它的英语只有其他所有的事情来在英语,但时间选择器来在西班牙语。谢谢。

x8goxv8g

x8goxv8g1#

您可以在初始化时间选取器对话框时传递语言环境,如下所示

Calendar.getInstance(Locale.US)
6gpjuf90

6gpjuf902#

仔细检查代码中的setDefaultLanguage()方法。ifelse if的条件看起来是一样的,所以它总是运行else if部分中的函数。
尝试对else if使用其他条件
查看此处的示例代码,

private fun setDefaultLanguage() {
      if (appPreference.appLanguage == CONDITION_1) {
          val locale1 = Locale("en")
          Locale.setDefault(locale1)
          val config = requireContext().resources.configuration
          config.setLocale(locale1)
          requireContext().createConfigurationContext(config)
      } else if (appPreference.appLanguage == CONDITION_2) {
          val locale1 = Locale("ja")
          Locale.setDefault(locale1)
          val config = requireContext().resources.configuration
          config.setLocale(locale1)
          requireContext().createConfigurationContext(config)
      }
  }

在您的场景中,条件1和条件2是相同的,因此弹出窗口始终使用else if中的语言

jgwigjjp

jgwigjjp3#

您只需在TimePicker之前添加此声明

Locale.setDefault(Locale.FRANCE);

相关问题