DatePickerDialog月份名称显示M + 0 +月份号

时间:2019-05-21 10:17:30

标签: android dialog

我正在使用DatePickerDialog。但是月份名称显示错误,即不是显示2019年1月,而是显示2019 M01和2月为2019 M02,依此类推。 我的问题是,calendarView的默认视图显示的是这样的月份数字,我在此视图中未更改或设置任何内容,通常它必须显示类似:2019 January。

如何解决此问题?

我的布局:

MY LAYOUT IMAGE

<CalendarView
        android:id="@+id/calender"
        android:layout_width="320dp"
        android:layout_height="300dp"/>

我的代码:

  CalendarView calendarView = view.findViewById(R.id.calender);
    calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            saveDate(year, month, dayOfMonth);
        }
    });

2 个答案:

答案 0 :(得分:0)

请遵循

public class MainActivity extends AppCompatActivity {

    Calendar calendar;
    CalendarView calendarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        calendar = Calendar.getInstance();


        calendar.set(Calendar.MONTH, Calendar.MAY);
        calendar.set(Calendar.DAY_OF_MONTH, 9);
        calendar.set(Calendar.YEAR, 2019);


        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.YEAR, 1);


        calendarView = findViewById(R.id.calendarView);


        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {

                String msg = "Selected date Day: " + i2 + " Month : " + (i1 + 1) + " Year " + i;
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();


            }
        });


    }
}

答案 1 :(得分:0)

这是由于语言环境配置错误而发生的,您必须将“语言环境”设置为 en 才能获得像月份一样的一月

以下代码将用于设置语言环境

fun setLocale(context: Context, language: String): Context {
        persist(language, context)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else updateResourcesLegacy(context, language)
    }

@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)
    val configuration = context.resources.configuration
    configuration.setLocale(locale)
    return context.createConfigurationContext(configuration)
}

private fun updateResourcesLegacy(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)
    val resources = context.resources
    val configuration = resources.configuration
    configuration.locale = locale
    resources.updateConfiguration(configuration, resources.displayMetrics)
    return context
}

基本活动中的某处

override fun attachBaseContext(newBase: Context) {
    super.attachBaseContext(setLocale(context, "en"))//or other language
   }