Android当前年份和月份在微调器

时间:2013-09-28 20:16:28

标签: android date spinner

使用此代码我在spinner中查看当前年份,现在我也希望看到当前月份。怎么样?感谢

    if (years.size() == 0){
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int currentMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
        years.add(currentYear+"/"+currentMonth);
    }

    SpinnerAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, years);

    yearSelector.setAdapter(adapter);
    yearSelector.setSelection(0);
    yearSelector.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,
                int position, long id) {
            String anno = years.get(position);

            showDetails(anno);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            return;
        }

1 个答案:

答案 0 :(得分:1)

您可以通过

获取当前月份
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
int currentMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
years.add(currentYear+"/"+currentMonth);

因为月份是基于android的零,所以你必须加1以获得当前月份